I have homework that should use fork() and wait() system calls:
1) Write two C programs using the fork() system call that generate the following in the child process:
a) Fibonacci sequence for m number provided in the command line (what is the only task I actually did:)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
int a=0, b=1, n=a+b,i,ii;
pid_t pid;
printf("Enter the number of a Fibonacci Sequence:\n");
scanf("%d", &ii);
if (ii < 0)
printf("Please enter a non-negative integer!\n");
else {
pid = fork();
if (pid == 0)
{
printf("Child is producing the Fibonacci Sequence...\n");
printf("%d %d",a,b);
for (i=0;i<ii;i++)
{
n=a+b;
printf("%d ", n);
a=b;
b=n;
}
printf("Child ends\n");
}
else
{
printf("Parent is waiting for child to complete...\n");
wait(NULL);
printf("Parent ends\n");
} } return 0; }
b) quicksort of the n numbers provided from the command line (I don’t know if I just have to write quicksort algorithm or implement fork() or I don’t know anymore?)
For both programs have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line.
2) Merge the above two programs (this is what about I have no idea how to do) into one, where one parent process will fork two child processes, one for the Fibonacci sequence, and the other for quicksort. The input to your program should be taken from command line arguments. Your source program must be named as merge.c. The command line looks like the following:
merge m n
Here, m and n are two positive integers. Thus your program should generate m random integers (this is what I also have no idea, generating random numbers in C) and sort them with quicksort, and compute the n-th Fibonacci number. The output should be as follows:
*Quicksort Process Started
Random Numbers Generated
6 8 3 10 25 5 2 30
Sorted Sequence
2 3 5 6 8 10 25 30
Quicksort Process Exits*
I need quick help 🙁 thank youuuu 🙂
Your first program seems basically correct. You’ll make your life a little easier if you make the Fibonacci part its own function.
Your quicksort program should be written the same way. Call
forkand have the child do the sorting.To generate random numbers, use the ‘rand’ function. (Same in C and C++.)