Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <stdio.h>
- using namespace std;
- void firstFork();
- void secondFork();
- void parentTask();
- void execTask();
- void countingTask();
- void firstFork()
- {
- pid_t pid = fork();
- if (pid == 0)
- {
- // child process
- cout << "Child 1 created by fork \n";
- execTask();
- }
- else if (pid < 0)
- {
- // fork failed
- cout << "First child fork failed \n";
- exit(EXIT_FAILURE);
- }
- }
- void secondFork()
- {
- pid_t pid = fork();
- if (pid == 0)
- {
- // child process
- cout << "Child 2 created by fork \n";
- countingTask();
- }
- else if (pid < 0)
- {
- // fork failed
- cout << "Second child fork failed \n";
- exit(EXIT_FAILURE);
- }
- }
- int main()
- {
- parentTask();
- }
- /*Forks itself 2 times say bye and finish immidiately */
- void parentTask()
- {
- // execl("/bin/ls","ls", (char *) 0);
- firstFork();
- // secondFork();
- cout << "Bye\n";
- exit(EXIT_SUCCESS);
- }
- /*exec its command and finishs */
- void execTask()
- {
- execl("/bin/ls", "ls", NULL);
- exit(EXIT_SUCCESS);
- }
- /*spawn 5 threads which would count to 30
- (by printing their name and counter to console output)
- and then finish. Numbers printed must be in order
- and each thread can print at most 1
- number more than other threads. */
- void countingTask()
- {
- exit(EXIT_SUCCESS);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement