6. processes and threads

2,128 views

Published on

Processes and Threads
Inter processes communication

Published in: Education, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,128
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
176
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

6. processes and threads

  1. 1. 6 – Processes and Threads Borislav VaradinovMarian Marinov SystemCEO of 1H Ltd. Administratormm@1h.com bobi [ at ] itp.bg
  2. 2. What is a Process?
  3. 3. What is a Process?➢ Single process OS – Arduino➢ Multy process OS – Any modern kernel
  4. 4. What is a Process?MEMORY CPU I/O APPLICATION
  5. 5. Multiple Processes?MEMORY MEMORY CPU I/O APPLICATION APPLICATION
  6. 6. Multiple Processes?➢ Segmentation Fault➢ Bus Error➢ Access violation SEGFAULT/SIGSEGV➢ Since Linux 3.2 CROSS MEMORY ATTACH
  7. 7. What is a Thread?
  8. 8. What is a Thread?MEMORY CPU I/O Thread0 Thread1 Process
  9. 9. What is a Thread?
  10. 10. Creating a process➢ FORK ➢ Copy the memory of the parent ➢ Inherit FD table ➢ Inherit credentials ➢ Inherit security➢ EXEC ➢ Create new memory space ➢ Inherit FD table ➢ Inherit credentials
  11. 11. What is a Thread?
  12. 12. Creating a process#include <stdio.h>#include <unistd.h>int main() { int i; pid_t p; p = fork(); if (p == 0) { for (i=0;i<=3;i++) printf("Im the child(%d)!n", p); } else { for (i=0;i<=3;i++) printf("My child is %dn", p); } return 0;}
  13. 13. Creating a processhackman@terion:~$ ./fMy child is 3721My child is 3721My child is 3721My child is 3721Im the child(0)!Im the child(0)!Im the child(0)!Im the child(0)!hackman@terion:~$
  14. 14. Creating a process#include <stdio.h>#include <unistd.h>int main() { int i; pid_t p; p = fork(); if (p == 0) { for (i=0;i<=3;i++) printf("Im the child(%d)!n", getpid()); } else { for (i=0;i<=3;i++) printf("My child is %dn", p); } printf("This is not good(%d)!n", getpid()); return 0;}
  15. 15. Creating a processhackman@terion:~$ ./fMy child is 3762My child is 3762My child is 3762My child is 3762This is not good(3761)!Im the child(3762)!Im the child(3762)!Im the child(3762)!Im the child(3762)!This is not good(3762)!hackman@terion:~$
  16. 16. Creating a processEXEC functions int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg, ..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execvpe(const char *file, char *const argv[], char *const envp[]);Exec functions does not return
  17. 17. Creating a processsystem() = exec(/bin/sh -c CMD)Which means:your process - exec /bin/sh - exec CMD
  18. 18. Types of processes➢ Foreground➢ Background➢ Daemons ➢ co-relation with terminal
  19. 19. Foreground processes➢ It has access to the terminals ➢ STDIN –0 ➢ STDOUT – 1 ➢ STDERR – 2➢ It is directly controlled by the user➢ It is connected to the terminal (text orgraphic)
  20. 20. Background processes➢ It has access only to the terminals ➢ STDOUT – 1 ➢ STDERR – 2➢ It is NOT directly controlled by the user➢ It is connected to the terminal (text orgraphic)
  21. 21. Daemon processes➢ Its STDIN/OUT/ERR are redirected to files➢ It is NOT directly controlled by the user➢ It is NOT connected to the terminal (text orgraphic)
  22. 22. IPC➢ FS PIPES➢ Process PIPES➢ Unix Domain Socket➢ Shared Memory SHM (SysV/POSIX)➢ Message Queues (SysV/POSIX)➢ Semaphores (SysV/POSIX)➢ Signals
  23. 23. IPC – process PIPEsProc 1 a = open(PIPE, “r”); b = open(PIPE, “w”);Proc 2 close(a); close(b); a = open(PIPE, “w”); b = open(PIPE, “r”);
  24. 24. IPC – Shared Memory Proc 1 SHMshmid = shmget(IPC_PRIVATE, ...);mem = shmat(shmid, ...); Proc 2memset(mem, 0, 200);
  25. 25. IPC – Message Queues Proc 1 Proc 2
  26. 26. IPC – Semaphores Proc 1 Proc 2
  27. 27. IPC – Signals Proc 1 Proc 2
  28. 28. Signals - Kill Proc 1 Proc 2
  29. 29. Signals – Signal handlersProc 1 sig_handler main() init_some() do_some() log_some()
  30. 30. Signals – Signal handlers Proc 1 sig_handler main() SIGHUP init_some()0 - 255 do_some() log_some()
  31. 31. Processes and Threads
  32. 32. Free Trainings @ Telerik Academy C# Programming @ Telerik Academy  Telerik Software Academy  Telerik Academy @ Facebook  Telerik Software Academy Forums 

×