thread and process

thread and process

October 08, 2021

Thread

  • smallest sequence of programmed instructions that can be managed independently by a scheduler.
  • Has its own registers, PC. SP.

Process

  • Instance of a computer program that is being executed
  • A process can have one or more threads

Parallel Computing

  • Run programs on one or more CPUs
  • Multi-threading (shared memory)
  • Multi-processing (Independent-memory)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a=1;
void run(){ a += 1; }

int main()
{
std::thread thread1(run);
std::thread thread2(run);

thread1.join(); //only return after the thread1 finish
thread2.join();

// only main thread will execute
std::cout<<a<<endl;
return 0;
}

Multithreading:

image-20211008194305920

Shared memory, so variable can be accessed by both threads.

However, when multithreads write (non-atomic instruction) a shared variable, inconsistency could happen.

Problem:Access shared resources:

CPU may process instructions randomly.

1
a += 1;//non-atomic
1
2
3
LOAD a,r
INC r
STORE r,a
1
2
3
4
5
6
LOAD a,r1
LOAD a,r2
INC r1
STORE r1,a
INC r2,
STORE r2,a
1
2
3
4
5
6
7
LOAD a,r1
INC r1
STORE r1,a
// this is sequential
LOAD a,r2
INC r2
STORE r2,a

To fix:

1
2
3
4
5
6
7
void run()
{
//thread blocking
l.lock();
a+=1;
l.unlock();
}

Note: only use lock when going to the critical section, becuase multithreading is much faster.

Communicate:

  • shared variable
  • semaphore, mutex, lock

Multiprocessing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a=1;
void run(bool child)
{
a+=child?2:1;
}

int main()
{
a+=1;
pid_t pid = fork();//create a parallel universe
// all processes will execute this
run(pid==0);
cour<<a;
}

image-20211008195339005

The pid returned by the fork() will not be 0 (0 as child process)

pid is the child process, should be 2+2;

pid is the parent process, should be 2+1;

Communicate:

  • shared memory
  • pipe
  • socket
  • RPC