Answer the question
In order to leave comments, you need to log in
How to read from named streams?
Hello. In front of me is the back to read two named streams and write to the third (created by me) the sum of the numbers that will come.
For this I wrote the following.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#define SHMSZ 1000
int main(int argc, char *argv[]) {
int num1, num2;
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);
int shmid,shmid1,shmid2;
key_t key;
int *shm1, *shm2, *shm3, *temp;
key = 54321;
printf("%d\n", key);
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0660)) < 0) {
return 1;
}
if ((shm1 = (int *)shmat(shmid, NULL, 0)) == (int *) -1) {
return 1;
}
if ((shm2 = (int *)shmat(num1, NULL, 0)) == (int *) -1) {
return 1;
}
if ((shm3 = (int *)shmat(num2, NULL, 0)) == (int *) -1) {
return 1;
}
int count = 0;
while (count < 100) {
temp[count] = shm2[count] + shm3[count];
count++;
}
memcpy(shm1, temp, (sizeof(temp)*10) );
if(shmdt(shm1) < 0){
return 1;
}
printf("%d\n",key);
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question