Credit:15 points Objective: This program assignment is given to the Operating Sy
ID: 3723315 • Letter: C
Question
Credit:15 points Objective: This program assignment is given to the Operating Systems course to allow the students to figure out how a single process (parent process) creates a child process and how they work on Unix/Linux/Mac OS X/Windows) environment. Additionally, student should combine the code for describing inter-process communication into this assignment. Both parent and child processes interact with each other through shared memory- based communication scheme or message passing scheme Environment: Unix/Linux environment (VM Linux or Triton Server, or Mac OS X), Windows platform Language: C or C+, Java Requirements: i. You have wide range of choices for this assignment. First, design your program to explain the basic concept of the process management in Unix Kernel. This main idea will be evolved to show your understanding on inter-process communication, file processing, etc. ii. Refer to the following system calls: fork). getpido. family of exec, wait), sleep) system calls for process management shmget( shmat), shmdt0, shmctlO for shared memory support or - msgget), msgsnd0. msgrev0, msgctila, etc. for message passing support ii. The program should present that two different processes, both parent and child, execute as they are supposed to iv. The output should contain the screen capture of the execution procedure of the program. v. Interaction between parent and child processes can be provided through inter-process communication schemes, such as shared-memory or message passing schemes. vi. Result should be organized as a document which explains the overview of your program, code, execution results, and the conclusion including justification of your program, lessons you've learned, comments, etc. Note: i. Please try to understand how the local and global variables work across the processes i. read) or write ) functions are used to understand how they work on the different processes. ii. For extra credit, you can also incorporate advanced features, like socket or thread functions, into your code.Explanation / Answer
//fork() is used to create child process . Child process is exact replica of parent process .. IN terms of code ,,both have same copy of code in the memory but two different processes. Statements after the fork repeat twice once child created. But we can control the execution in the program by using the return value of the fork which is greater than 0(child id will be returned to parent when child process crated using fork()).. In child process ,0 will be returned. So using these conditions we can control the execution of child and parent process . Child and parent have the copies of local and global variables. If child modifies one variable ,parent can not see that change and vice versa. Hence we need to use interprocess communication to read any values between child and parent.There are different ways of interprocess communications like using pipe, message queues, shared memory etc.
-------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main()
{
char *ptr;
int shmid,pid;
key_t key;
key = 1000;
//create shared memory
shmid = shmget(key,20*sizeof(char),IPC_CREAT|0666);
if(shmid < 0 )
{
perror("Error creating shared memory: ");
return -1;
}
//allocate memory for ptr
ptr = (char*)shmat(shmid,(char*)0,0);
//create child process
pid = fork();
if( pid == 0 ) //its child process
{
//copy some string to ptr so that parent can read this message
strcpy(ptr,"Hello World!!");
printf("Child with pid %d writes %s word into shared memory ",getpid(),ptr);
}
else //parent process
{
wait(0); //wait fir child to finish
//after child copies the message Hello world ,,parent will be given time for execution
printf("Parent with pid %d reads from shared memory: %s ",getpid(),ptr);
}
//destroy shared memory
shmctl(shmid,IPC_RMID,0);
return 0;
}
---------------------------------------------------------------------------------------
//output:
Child with pid 4 writes Hello World!! word into shared memory
Parent with pid 3 reads from shared memory: Hello World!!
--------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.