Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: The conjecture states that when this algorithm

The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm:

The conjecture states that when this algorithm is continually applied all positive integers will eventually reach 1. Write a C program using fork() system call that generates this sequence in the child process. The starting number will be provided from the command line. Because the parent and the child processes have their own copies of the data.

----------------THE PROGRAM MUST USE MESSAGE QUEUES------------------------

Another approach to designing this program is to create message queues, one for the parent process and one for the child process. The parent can write a message containing the starting number of the sequence to the childs message queue. After the child process receives the message, the child process will generate the sequence, and send the entire sequence to the parents message queue. The parent will receive the sequence from its message queue, then output the sequence to the screen. Before the processes terminate, they will each destroy its own message queue.

----------------THE PROGRAM MUST USE MESSAGE QUEUES------------------------

One area of concern with cooperating processes involves synchronization issues. In this exercise, the parent and child processes must be coordinated so that the parent does not output the sequence until the child finishes execution. These two processes will be synchronized using synchronous message sending and receiving.

Please read carefully if you are willing to help me.

I have already submitted a fork and collatz program. (see below), I need to modify it to use msgQueues.

#include

#include

#include

#define MAX_SIZE1 20

#define MAX_SIZE2 10

typedef struct {

char name[MAX_SIZE1];

int scores[MAX_SIZE2];

} gradebook;

void printscores(int scoresarray[]){

int i;

for(i=0; i

printf("%d ", scoresarray[i]);

}

int scoresum(int sumarray[]){

int i, sum=0;

for(i=0; i

sum+=sumarray[i];

return sum;

}

int compare(const void * a, const void * b){

gradebook *p1 = (gradebook *)a;

gradebook *p2 = (gradebook *)b;

int suma = scoresum(p1->scores);

int sumb = scoresum(p2->scores);

return (sumb-suma);

}

int main(){

time_t start_t, end_t;

double sum_t;

time(&start_t);

int n;

printf("How many students are in the class? ");

scanf("%d", &n);

gradebook *students = (gradebook *)malloc(sizeof(gradebook)*n);

int i,j;

for(i=0; i

fflush(stdin);

printf("What are the students names? ");

scanf("%s", students[i].name);

for(j=0; j

students[i].scores[j] = rand()%51+50;

}

printf(" ");

}

qsort(students, n, sizeof(gradebook), compare);

for(i=0; i

printf("%s ", students[i].name);

printf("Total Score: %d ", scoresum(students[i].scores));

printscores(students[i].scores);

printf("------------------------------ ");

printf(" ");

}

free(students);

time(&end_t);

sum_t=difftime(end_t, start_t);

printf("Total Time to run program: %f ", sum_t);

return 0;

}

---------------------------------------------------

here is a short example of a message queue

// parent creates both message queues if ((msqidP = msgget(keyP, 0600 | IPC_CREAT)) < 0) { fprintf(stderr, "msgget failed "); return 1; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions