Skip to content

Instantly share code, notes, and snippets.

@JudahSan
Created June 28, 2024 15:47
Show Gist options
  • Save JudahSan/3982687881cd5a8acd6d5884e585fd49 to your computer and use it in GitHub Desktop.
Save JudahSan/3982687881cd5a8acd6d5884e585fd49 to your computer and use it in GitHub Desktop.
Unix Threads in C notes

Processes and threads

  • A process represents an instance of a running program
  • Threads are individual units of execution within a process.

Process

  • A process is an active program, ie, a program that is under execution. It includes the program code, program counter, process stack, registers, etc
  • When a computer program is triggered to execute, it does not run directly, but it first determines the steps required for execution of the program, and following these steps of execution is referred to as a process.
  • An individual process takes its own memory space and does not share this space with other processes.
  • Clone process/ child process - is one which is created by another process
  • Parent process - this is the main process that is reponsible for creating other processes to perform multiple tasks at a time

Threads

  • Threads are individual units of execution within a process.
  • They share the same memory space and resources, and they enable concurrent execution.
  • In short, threads are lightweight processes that enable multitasking within a single program.
Comparison Basis Process Thread
Definition A process is a program under execution i.e. an active program. A thread is a lightweight process that can be managed independently by a scheduler
Context switching time Processes require more time for context switching as they are heavier. Threads require less time for context switching as they are lighter than processes.
Memory Sharing Processes are totally independent and don’t share memory. A thread may share some memory with its peer threads.
Communication Communication between processes requires more time than between threads. Communication between threads requires less time than between processes.
Blocked If a process gets blocked, remaining processes can continue execution. If a user level thread gets blocked, all of its peer threads also get blocked.
Resource Consumption Processes require more resources than threads. Threads generally need less resources than processes.
Dependency Individual processes are independent of each other. Threads are parts of a process and so are dependent.
Data and Code sharing Processes have independent data and code segments. A thread shares the data segment, code segment, files etc. with its peer threads.
Treatment by OS All the different processes are treated separately by the operating system. All user level peer threads are treated as a single task by the operating system.
Time for creation Processes require more time for creation. Threads require less time for creation.
Time for termination Processes require more time for termination. Threads require less time for termination.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
int main(int argc, char* argv[]) {
int x = 5;
int pid = fork();
if (pid == -1) {
return 1;
}
if (pid == 0) {
x++;
}
sleep(2);
printf("Value of x: %d\n", getpid());
if (pid != 0) {
wait(NULL);
}
return 0
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int x = 3;
void* routine() {
x++;
sleep(2);
printf("Value of x: %d\n", getpid());
}
void routine2() {
sleep(2);
printf("Process ID %d\n", getpid());
}
int main(int argc, char* argv[]) {
pthread_t t1, t2;
if (pthread_create(&t1, NULL, &routine, NULL) !=0) {
return 1;
}
if (pthread_create(&t2, NULL, &routinee, NULL) !=0) {
return 2;
}
if (ppthread_join(t1, NULL) !=0) {
return 3;
}
if (pthread_join(t2, NULL) !=0) {
return 4;
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* routine() {
printf("Test from threads\n");
sleep(3);
printf("Ending thread\n");
}
int main(int argc, char* argv[]) {
pthread_t t1, t2;
if (pthread_create(&t1, NULL, &routine, NULL) !=0) {
return 1;
}
if (pthread_create(&t2, NULL, &routine, NULL) !=0) {
return 2;
}
if (ppthread_join(t1, NULL) !=0) {
return 3;
}
if (pthread_join(t2, NULL) !=0) {
return 4;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment