(Solved) : Comment C Code Much Detail Possible Describe Program Code Include Include Include Include Q43931950 . . .
Comment this C code With as much detail as possible and describewhat the program is doing
CODE:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <signal.h>
int id;
struct Mesg{//struct that containsmessage,count, and token
char Message[40];
int count;
char token[10][20];
char sorttoken[10][20];
};
#define KEY ((key_t)(713))
#define SEGSIZE sizeof(struct Mesg)
void cleanup(int sigtype){
shmdt(0);
printf(“n Terminatingn”);
execlp(“ipcrm”,”ipcrm”,”-M”,”713″,(char*)(0));
printf(“ERROR!”);
exit(0);
}
int main(){
int i,j;
int count;
char *token;
char tmp[20];
struct Mesg *msg;
struct shmid_ds shmbuf;
signal(SIGINT,cleanup);
signal(SIGQUIT,cleanup);
id=shmget(KEY,SEGSIZE,IPC_CREAT|0666);
if(id<0){
perror(“shmget failed 1n”);
exit(1);
}
msg=(struct Mesg*) shmat(id,0,0);
if(msg<=(struct Mesg*)(0)){
perror(“shma failed 2n”);
exit(1);
}
msg->count=0;
printf(“Parent : Enter a Message : n”);
count=read(0,msg->Message,40);
if(count<=0){
printf(“read failed or no text entered!n”);
exit(0);
}
msg->Message[count-1]=’ ‘;
printf(“Parent pid = %d, Messagereaded:’%s’n”,getpid(),msg->Message);
printf(“Just before forking, pid = %dn”,getpid());
if(fork()==0){
printf(“fork 1n”);
msg->count=1;
for(i=0;i<count;i++)
if(msg->Message[i]==’ ‘)
msg->count++;
printf(“token count = %dn”,msg->count);
exit(0);
}else
if(fork()==0){
sleep(1);
printf(“fork 2n”);
i=0;
token=strtok(msg->Message,” “);
while(token!=NULL){
strcpy(msg->token[i],token);
i++;
token=strtok(NULL,” “);
}
printf(“Tokens : n”);
for(i=0;i<msg->count;i++){
printf(“%sn”,msg->token[i]);
}
printf(“Tokens endn”);
exit(0);
}else
if(fork()==0){
sleep(3);
printf(“fork 3n”);
for(i=0;i<msg->count;i++){
strcpy(tmp,msg->token[i]);
strcpy(msg->sorttoken[i],tmp);
}
for(i=msg->count-1;i>=0;i–){
for(j=0;j<i;j++){
if(strcmp(msg->sorttoken[j],msg->sorttoken[j+1])>0){
strcpy(tmp,msg->sorttoken[j]);
strcpy(msg->sorttoken[j],msg->sorttoken[j+1]);
strcpy(msg->sorttoken[j+1],tmp);
}
}
}
printf(“Sorted tokens : n”);
for(i=0;i<msg->count;i++){
printf(“%sn”,msg->sorttoken[i]);
}
printf(“Sorted tokens endn”);
exit(0);
}
for(i=0;i<3;i++)
wait(0);
printf(“Press Ctrl-C (or) Ctrl- to clean up the memory andexit.n”);
for(;;);
return 0;
}
Expert Answer
Answer to Comment this C code With as much detail as possible and describe what the program is doing CODE: #include #include #incl…
OR