Operating Systems: Thread

Ahmad Yoosofan

https://yoosofan.github.io

University of Kashan

حالت‌های نخ

Python Thread (I)

1 def f1():
2   print("This is a test")
3 
4 print("main")
5 f1()
6 print("After f1()")
1 import threading
2 
3 def f1():
4   print("This is a test")
5 
6 print("main")
7 th1 = threading.Thread(target=f1)
8 th1.start()
9 print("After th1.start()")
main
This is a test
After f1()
main
After th1.start()
This is a test
main
This is a test
After th1.start()
main
This is  After th1.start()
a test

Python main

1 def f1():
2   print("This is a test")
3 
4 if __name__ == "__main__":
5   print("main")
6   f1()
7   print("After f1()")
1 import threading
2 
3 def f1():
4   print("This is a test")
5 
6 if __name__ == "__main__":
7   print("main")
8   th1 = threading.Thread(target=f1)
9   th1.start()
10   print("After th1.start()")

Python Thread (II)

1 import threading
2 
3 def f1():
4   print("In f1")
5 
6 def f2():
7   print("In f2");
8 
9 if __name__ == "__main__":
10   th1 = threading.Thread(target=f1)
11   th2 = threading.Thread(target=f2)
12   print("main")
13   th1.start();
14   print("After f1")
15   th2.start();
16   print("After f2")
main
After f1
After f2
In f1
In f2
main
After f1
In f1
After f2
In f2
main
After f1
In f1
After f2
In f2
main
After f1
After f2
In f2
In f1
main
After In f1
After f2
In f2
f1

Python Thread (III)

1 import threading # src/thread/240.py
2 def f1():
3   for i in range(1000000):
4     print(i,"__111")
5 
6 def f2():
7   for i in range(1000000):
8     print(i,"__222")
9 
10 if __name__ == "__main__":
11   th1 = threading.Thread(target=f1)
12   th2 = threading.Thread(target=f2)
13   th1.start();
14   th2.start();
15   for i in range(1000000):
16     print(i, "__main")
python3 240.py > 240output65.txt
python3 240.py > 240output55.txt
python3 240.py > 240output45.txt
python3 240.py > 240output35.txt
python3 240.py > 240output25.txt
5314 __111
5315 __111 __main
1459 __main
1460 __main

240output55.txt : 9108
2139 __main
2140 __main
2141
2336 __222
2337 __222

240output55.txt : 9790
6059 __111
6060 __111 __main
2142 __main
2143 __main

240output55.txt : 11281
1589 __111
1590 __1110 __222
1 __222
2 __222

240output65.txt : 1590

Thread Python join(I)

1 import threading
2 def f1():
3   for i in range(10000):
4     print(i,"__111")
5 
6 if __name__ == "__main__":
7   th1 = threading.Thread(target=f1)
8   th1.start()
9   th1.join()
10   for i in range(10000):
11     print(i, "__main")
1 import threading
2 def f1():
3   for i in range(10000):
4     print(i,"__111")
5 
6 if __name__ == "__main__":
7   th1 = threading.Thread(target=f1)
8   th1.start();
9   for i in range(10000):
10     print(i, "__main")
11   th1.join()

Thread Python join(II)

1 import threading
2 def f1():
3   for i in range(51):
4     print(i,"__This is a test")
5 
6 def f2():
7   for i in range(50):
8     print(i,"__The second function is called");
9 
10 if __name__ == "__main__":
11   th1 = threading.Thread(target=f1)
12   th2 = threading.Thread(target=f2)
13   th1.start()
14   th2.start()
15   th1.join()
16   for i in range(1):
17     print(i, "__In main AAA");
18   th2.join()
19   for i in range(1):
20     print(i, "__In main BBB");
1 import threading
2 def f1():
3   for i in range(51):
4     print(i,"__This is a test")
5 
6 def f2():
7   for i in range(50):
8     print(i,"__The second function is called");
9 
10 if __name__ == "__main__":
11   th1 = threading.Thread(target=f1)
12   th2 = threading.Thread(target=f2)
13   th1.start()
14   th2.start()
15   th1.join()
16   th2.join()
17   for i in range(1):
18     print(i, "__In main");

Thread Python parameter

1 import threading
2 
3 def f1(j):
4   for i in range(51):
5     print(j,"__",i,"__This is a test")
6 
7 if __name__ == "__main__":
8   th1 = threading.Thread(target=f1, args=(1,))
9   th2 = threading.Thread(target=f1, args=(2,))
10   th1.start()
11   th2.start()
12   th1.join()
13   th2.join()
1 import threading
2 def f1(j):
3   for i in range(500):
4     print(j,"__",i,"__This is a test")
5 
6 if __name__ == "__main__":
7   th1 = threading.Thread(target=f1,args=(1,))
8   th2 = threading.Thread(target=f1,args=(2,))
9   #for i in range(10):  print('OOOOO__',i)
10   th1.start()
11   #for i in range(10):  print('LLLLL__',i)
12   th1.join()
13   for i in range(400):
14     print("iii__",i)
15   th2.start()
16   th2.join()

Thread Python Sharing

1 import threading , time
2 def f1(i):
3   for j in range(10):
4     for k in range(50):
5       print(i, "__", j, "__", k, "__1__f1")
6       print(i, "__", j, "__", k, "__2__f1")
7     time.sleep(0.1);
8 
9 th1 = threading.Thread(target=f1,args=(1,))
10 th2 = threading.Thread(target=f1,args=(2,))
11 th3 = threading.Thread(target=f1,args=(3,))
12 th1.start()
13 th2.start()
14 th3.start()
15 for k in range(10):
16   for i in range(50):
17     print(i, "__main_before");
18     print(i, "__main_after");
19   time.sleep(0.1);
20 th1.join()
21 th2.join()
22 th3.join()
1 import threading, time
2 class MyShare:  
3   counter = 0; end = 120;  array = [0] * end
4 def f1(sh1):
5   sh1.array[0] = 1;
6   for i in range(5000):pass #time.sleep(1)
7   for i in range(5):  print("1_1_", i)
8   for i in range(10000):pass
9   for i in range(5):  print("1_2_", i)
10   sh1.array[1] = 4;
11 def f2(sh2):
12   sh2.array[2] = 8;
13   for i in range(5000):pass #time.sleep(1)
14   for i in range(5):  print("2_1_", i)
15   for i in range(10000):pass
16   for i in range(5):  print("2_2_", i)
17   sh2.array[3] = 16;
18 myShare = MyShare()
19 th1 = threading.Thread(target=f1, args=(myShare,))
20 th2 = threading.Thread(target=f2, args=(myShare,))
21 th1.start(); th2.start(); #time.sleep(1)
22 for i in range(7): 
23   for k in range(100000): pass
24   print(myShare.array[i])
25 th1.join(); th2.join();

Create Concurrent / Parallel Code

C++ Thread

1 #include<iostream>
2 #include<thread> // 620.cpp
3 using namespace std;
4 // g++ -std=c++17 -pthread 
5 // clang++ 630.cpp --std=c++17 -lpthread
6 void f(void){
7   cout << "In thread " << endl;
8   cout << "the next output in thread" << endl;
9 }
10 
11 int main(){
12   thread t(f);
13   cout << "In main" << endl;
14   cout << "second in main" << endl;
15   t.join();
16 }
thread$ ./a.out
In main
second in main
In thread
the next output in thread
thread$ ./a.out

C++ Thread join

1 #include<iostream>
2 #include<thread>
3 using namespace std;
4 // g++ -std=c++17 -pthread 
5 void f(void){
6   cout << "In thread " << endl;
7   cout << "the next output in thread" << endl;
8 }
9 
10 int main(){
11   thread t(f);
12   cout << "In main" << endl;
13   cout << "second in main" << endl;
14 }
thread$ g++ -std=c++17 -pthread 630.cpp 
thread$ ./a.out
In main
second in main
terminate called without an active exception
Aborted (core dumped)
thread$ 

Array of Thread

1 #include<iostream>
2 #include<thread>
3 using namespace std;
4 void f(void){
5   cout << "In thread " << endl;
6   cout << "the next output" << endl;
7 }
8 int main(){
9   const int N = 4;
10   thread t[N];
11   for(int i = 0; i < N; i++) 
12     t[i]=thread(f);
13   cout << "In main" << endl;
14   cout << "second in main" << endl;
15   for(int i=0;i<N;i++)
16     t[i].join();
17 }
thread$ ./a.out
In thread 
the next outputIn thread 
In thread 
the next output
In main
second in main
In thread 
the next output
the next output

thread$

Array of Thread output

thread$ ./a.out
In thread 
the next outputIn thread 
In thread 
the next output
In main
second in main
In thread 
the next output
the next output

thread$
thread$ ./a.out
In thread In main
In thread 

In thread In thread 

the next output
the next outputthe next output
second in main
the next output

thread$ 

Array of Thread output(II)

thread$ ./a.out
In thread In main

second in main
In thread 
the next output
In thread 
the next output
the next output
In thread 
the next output
thread$ 
thread$ ./a.out
In thread In main

second in main
In thread 
the next output
In thread 
the next output
the next output
In thread 
the next output
thread$ 

Array of Thread(III)

1 #include<iostream>
2 #include<thread>
3 using namespace std; // g++ -std=c++17 -pthread 650.cpp 
4 void f(int i){
5   cout << "In thread : " << i << endl;
6   cout << "the next output" << endl;
7 }
8 int main(){
9   const int N = 4;
10   thread t[N];
11   for(int i = 0; i < N; i++) 
12     t[i]=thread(f, i);
13   cout << "In main" << endl;
14   cout << "second in main" << endl;
15   for(int i=0;i<N;i++)
16     t[i].join();
17   return 0;
18 }
thread$ ./a.out
In thread In main

second in main
In thread 
the next output
In thread 
the next output
the next output
In thread 
the next output
thread$ 

Array of Thread(III)

thread$ ./a.out
In thread : In thread : 02In main
the next output

the next output
In thread : 3
the next output
In thread : 1
the next output

second in main
thread$ 
thread$ ./a.out
In thread : In thread : 0In thread : 2In main
the next output

second in main

the next output
In thread : 3
the next output
1
the next output
thread$ 

Array of Thread(III)

thread$ ./a.out
In thread : In mainIn thread : In thread : 23
0
the next output
second in main

the next output
In thread : 1
the next output

the next output
thread$ 
thread$ ./a.out
In thread : In main0
In thread : 1
the next outputIn thread : 
the next output
In thread : 2
the next output
3
the next output

second in main
thread$ 

Threads and Processes

Many to One Thread

Different Types of Threads

User and Kernel Thread

ارتباط میان حالت‌های نخ‌ها

سرویس دهندهٔ وب با چند نخ

Threads in Webserver

End

1