Implement inter process communication (between parent process and child process)
ID: 3601239 • Letter: I
Question
Implement inter process communication (between parent process and child process) using shared memory in C language
Explanation / Answer
#include #include #include #include #include #define MAX_SEQUENCE 10 typedef struct { long fib_sequence[MAX_SEQUENCE]; int sequence_size; } shared_data; void child_func(shared_data* dataPtr); void main(int argc,char *argv[]){ int shmID,size,status,i; shared_data* dataPtr; pid_t pid; if((pid=fork())0){ // Parent Process if((shmID = shmget(IPC_PRIVATE, MAX_SEQUENCE*sizeof(long), IPC_CREAT | 0666))sequence_size = atoi(argv[1]); if((dataPtr->sequence_size) < 0){ printf("You entered an invalid(negative) size number "); exit(0); } else if((dataPtr->sequence_size) > MAX_SEQUENCE){ printf("Please enter a value less than MAX_VALUE "); exit(0); } wait(status); // Wait untill child finishes its job for(i=0; isequence_size; i++) printf("%ld ", dataPtr->fib_sequence[i]); printf(" "); shmdt((void *) dataPtr); shmctl(shmID,IPC_RMID,NULL); } else{ // Child Process child_func(dataPtr); exit(0); } } void child_func(shared_data* dataPtr){ int index; printf("I am in Child Process "); printf("Size of array %d ", dataPtr->sequence_size); dataPtr->fib_sequence[0]; if((dataPtr->sequence_size) > 0){ dataPtr->fib_sequence[1]=1; for(index=2; index sequence_size; index++) dataPtr->fib_sequence[index] = dataPtr->fib_sequence[index-1] + dataPtr->fib_sequence[index-2]; } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.