-
Notifications
You must be signed in to change notification settings - Fork 0
/
Write a program to create processes and threads.c
54 lines (44 loc) · 1.3 KB
/
Write a program to create processes and threads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
// Function to be executed by the thread
void* thread_function(void* arg) {
printf("Thread: Hello from the thread!\n");
return NULL;
}
int main() {
pid_t pid;
pthread_t thread;
// Create a new process
pid = fork();
if (pid < 0) {
// Fork failed
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
printf("Child Process: Hello from the child process!\n");
// Create a new thread in the child process
if (pthread_create(&thread, NULL, thread_function, NULL) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
// Wait for the thread to finish
if (pthread_join(thread, NULL) != 0) {
perror("pthread_join");
exit(EXIT_FAILURE);
}
printf("Child Process: Thread has finished execution.\n");
} else {
// Parent process
printf("Parent Process: Hello from the parent process!\n");
// Wait for the child process to finish
if (wait(NULL) < 0) {
perror("wait");
exit(EXIT_FAILURE);
}
printf("Parent Process: Child process has finished execution.\n");
}
return 0;
}