Need C++ Or Java Please

Program Assignment : Process Management

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(), getpid(), family of exec(), wait(), sleep() system calls for process management

– shmget(), shmat(), shmdt(), shmctl() for shared memory support or

– msgget(), msgsnd(), msgrcv(), msgctl(), etc. for message passing support

iii. 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. In addition, please try to understand how the local and global variables work across the processes

ii. read() or write () functions are used to understand how they work on the different processes.

iii. For extra credit, you can also incorporate advanced features, like socket or thread functions, into your code.

Examples:

1. Process Creation and IPC with Shared Memory Scheme

=============================================================

#include <stdio.h>

#include <sys/shm.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

int main(){

pid_t pid;

int segment_id; //allocate the memory

char *shared_memory; //pointer to memory

const int size = 4096;

segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);

shared_memory = (char *) shmat(segment_id, NULL, 0);

pid = fork();

if(pid < 0) { //error

fprintf(stderr, “Fork failed”);

return 1;

}

else if(pid == 0){ //child process

char *child_shared_memory;

child_shared_memory = (char *) shmat(segment_id,NULL,0); //attach mem

sprintf(child_shared_memory, “Hello parent process!”); //write to the shared mem

shmdt(child_shared_memory);

}

else {//parent process

wait(NULL);

printf(“Child process completed.\n”);

printf(“The child process has written to the shared memory.\nIt has said: %s\n”, shared_memory); //read from shared mem

shmdt(shared_memory);

shmctl(segment_id, IPC_RMID, NULL);

}

return 0;

}

 

=============================================================

2. Process Creation and IPC with Message Passing Scheme

=============================================================

#include <stdio.h>

#include <stdlib.h>

#include <sys/shm.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <string.h>

#define N 20

typedef struct{

long stype;

char input[N];

}msgq;

int origin = 12345;

 

int main(int argv, char * argc[]){

msgq sendq;

msgq rcvq;

size_t qlen;

key_t key=1111;

pid_t pid_p, pid_c;

 

int msgid = msgget((key_t)1111, 0666| IPC_CREAT);  //declare the msg queue id

int shrm_id, shrm_size = 1024;                    //size of memory for sharing

char * shrm_addr;

 

shrm_id = shmget(IPC_PRIVATE, shrm_size, S_IRUSR | S_IWUSR);  // get the id of memory for sharing

shrm_addr = (char*)shmat(shrm_id, NULL, 0);

 

pid_p = getpid();             // store parent_process_id into variable ; pid_p

pid_c = fork();               // store child_process_id into variable ; pid_c

 

//meaning of return value of fork()

//value < 0 : fork is failed

//value == 0 : child process

//value > 0 : parent process

if(pid_c < 0){

fprintf(stderr, “failed to fork()\n”); // print error msg by using stderr of fprintf

return 0;

}else if(pid_c ==0){

printf(“start of child process\n”);

printf(“——————————–\n\n”);

 

printf(“origin : %d\n”, origin);

origin++;

printf(“value of origin after origin++; : %d\n\n”, origin);

 

char * c_shrm_addr = (char *)shmat(shrm_id, NULL, 0);  // give shared memory to child by using shmat()

// NULL means find somewhere which has been mapped

if(c_shrm_addr ==(void *) -1)  // shmat() return (void *)-1 when the sharing is failed

fprintf(stderr, “failed to share\n”);

pid_c = getpid();

 

sendq.stype=1;

 

printf(“input string that will be sent to parent process : “);

scanf(“%s”,sendq.input);   //store the string for send to parent into msg queue

 

qlen= strlen(sendq.input)+1;

 

if(msgsnd(msgid,&sendq, qlen ,IPC_NOWAIT)<0){

perror(“msgsnd”);

exit(0);

}

else{

printf(“Msgsend : %s\n”, sendq.input);

}

printf(“\n——————————–\n”);

printf(“end of child process  \n”);

 

} else {

//now in parent process

 

printf(“\nstart of parent process\n”);

printf(“——————————–\n\n”);

 

char str[N];

int i = 0;

wait(NULL);       //wait until the child process finished

 

printf(“\n——————————–\n”);

printf(“back to the parent process  \n”);

printf(“——————————–\n\n”);

 

printf(“origin : %d\n”, origin);

if(origin == 12345)

printf(“changed origin value in child is not applied to parent\n\n”);

if(origin == 12346)

printf(“changed origin value in child is applied ro parent\n\n”);

 

if((msgid = msgget(key,0666))<0){      //in case of error in msgget

perror(“msgget”);

exit(1);

}

 

if(msgrcv(msgid,&rcvq,N+1,1,0)==0){     //in case of error in msgrcv

perror(“msgrcv”);

exit(1);

}

 

printf(“recieve : %s\n\n”,rcvq.input);

printf(“child process refers to the memory of parent : %x\n”, (int)shrm_addr);

 

pid_p = getpid(IPC_PRIVATE, IPC_CREAT);

 

printf(“ID of Parent : %d\n”, pid_p);

printf(“ID of Child  : %d\n”, pid_c);

 

printf(“\n——————————–\n”);

printf(“end of parent process\n\n”);

}

 

shmdt(shrm_addr);

shmctl(shrm_id, IPC_RMID, NULL);

 

return 0;

}

 

============================================================= 3. Compiling and executing C codes

$ cc msgpass.c -o msgpass

$ ./msgpass

Needs help with similar assignment?

We are available 24x7 to deliver the best services and assignment ready within 3-4 hours? Order a custom-written, plagiarism-free paper

Get Answer Over WhatsApp Order Paper Now