0% found this document useful (0 votes)
11 views6 pages

229J07 Report

The document outlines an experimental report for a Computer Science & Technology course, focusing on process and thread management in programming. It includes objectives and two main programming tasks: one using fork() and wait() for process creation and management, and another using pthread_create() and pthread_join() for thread management. Sample code for both tasks is provided, demonstrating the creation and termination of processes and threads.

Uploaded by

mazineanas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

229J07 Report

The document outlines an experimental report for a Computer Science & Technology course, focusing on process and thread management in programming. It includes objectives and two main programming tasks: one using fork() and wait() for process creation and management, and another using pthread_create() and pthread_join() for thread management. Sample code for both tasks is provided, demonstrating the creation and termination of processes and threads.

Uploaded by

mazineanas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Report 1

Experimental name: Process


Experimental type: design
Major : Computer Science&Technology
Class : 2022 Computer Science&Technology
Chinese name(NO.): 229J07 Mazin
Instructor: Jin Guiyue

Date :2025-03-27

Experimental objectives:
1.
2.

Experimental contents:
1. Write a program using fork(),wait() etc..
program code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
pid_t pid;

pid = fork(); // Create a new process

if (pid < 0) {
perror("Fork failed");
exit(1);
} else if (pid == 0) {
printf("Child Process: PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(2);
printf("Child Process terminating...\n");
} else {
printf("Parent Process: PID = %d, Child PID = %d\n", getpid(), pid);
wait(NULL); // Wait for child process to finish
printf("Parent Process resuming after child termination.\n");
}

return 0;
}
screenshot
2. Write a program using pthread_create(), pthread_join() etc..
program code: and

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

// Function to be executed by threads


void* threadFunction(void* arg) {
printf("Thread %d is running\n", *(int*)arg);
sleep(2);
printf("Thread %d has finished execution.\n", *(int*)arg);
return NULL;
}

int main() {
pthread_t thread1, thread2;
int id1 = 1, id2 = 2;

pthread_create(&thread1, NULL, threadFunction, &id1);


pthread_create(&thread2, NULL, threadFunction, &id2);

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

printf("All threads have completed execution.\n");


return 0;
}

screenshot:

You might also like