Question

In: Computer Science

You will write a C program to fork a child process. The child process will print...

You will write a C program to fork a child process. The child process will print the current time/date. The parent will wait for the child to complete.

Here is how you can programmatically call the Linux date command from a C program.

execlp("/bin/date","date",NULL);

Your C file should be named myfork.c
The binary will be named myfork.

Solutions

Expert Solution

Code in C

#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
//Main function
int main()
{
//pid_t is a datatype used for representing the process id
pid_t cpid;
//creating two proccess parent and child using fork()
//checking if the process is child
if (fork()== 0)
{
//we can identify the process by checking the pid_t if it is 0 then it is a child process
//printing the date
printf(" ",execlp("/bin/date","date",NULL));
// terminate child
exit(0);
}
//checking if the process is parent
else
//if so then waiting until the child process finish its execution by wait() function
cpid = wait(NULL);
return 0;
}

Screenshots

Input\output


Related Solutions

Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Write a program in C or C++ that spawns three child processes. The first child sends...
Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Write a C program that evaluates the factorials from 1 to 5. Print results in a...
Write a C program that evaluates the factorials from 1 to 5. Print results in a tabular format.
write a program that will print a truth table for p ^ ~q. Using C++ please.
write a program that will print a truth table for p ^ ~q. Using C++ please.
Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
Write a program to print 2 strings. One string must be written by parent process and...
Write a program to print 2 strings. One string must be written by parent process and another string must be written by a child process In language C
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT