6 – Processes
           and Threads


                 Borislav Varadinov
Marian Marinov
                            System
CEO of 1H Ltd.
                      Administrator
mm@1h.com
                   bobi [ at ] itp.bg
What is a Process?
What is a Process?



➢ Single process OS – Arduino
➢ Multy process OS – Any modern kernel
What is a Process?



MEMORY      CPU        I/O




         APPLICATION
Multiple Processes?
MEMORY     MEMORY      CPU             I/O




         APPLICATION         APPLICATION
Multiple Processes?

➢ Segmentation Fault
➢ Bus Error
➢ Access violation

            SEGFAULT/SIGSEGV

➢ Since Linux 3.2
    CROSS MEMORY ATTACH
What is a Thread?
What is a Thread?
MEMORY            CPU             I/O




    Thread0             Thread1


              Process
What is a Thread?
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
What is a Thread?
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("I'm the child(%d)!n", p);
    } else {
        for (i=0;i<=3;i++)
             printf("My child is %dn", p);
    }
    return 0;
}
Creating a process

hackman@terion:~$ ./f
My child is 3721
My child is 3721
My child is 3721
My child is 3721
I'm the child(0)!
I'm the child(0)!
I'm the child(0)!
I'm the child(0)!
hackman@terion:~$
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("I'm 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;
}
Creating a process

hackman@terion:~$ ./f
My child is 3762
My child is 3762
My child is 3762
My child is 3762
This is not good(3761)!
I'm the child(3762)!
I'm the child(3762)!
I'm the child(3762)!
I'm the child(3762)!
This is not good(3762)!
hackman@terion:~$
Creating a process

EXEC 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
Creating a process


system() = exec('/bin/sh -c CMD')

Which means:

your process
  - exec /bin/sh
     - exec CMD
Types of processes


➢ Foreground
➢ Background
➢ Daemons
 ➢ co-relation with terminal
Foreground processes

➢ It has access to the terminal's
 ➢ STDIN         –0
 ➢ STDOUT – 1
 ➢ STDERR – 2
➢ It is directly controlled by the user
➢ It is connected to the terminal (text or
graphic)
Background processes

➢ It has access only to the terminal's
 ➢ STDOUT – 1
 ➢ STDERR – 2
➢ It is NOT directly controlled by the user
➢ It is connected to the terminal (text or
graphic)
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 or
graphic)
IPC

➢ FS PIPES
➢ Process PIPES
➢ Unix Domain Socket
➢ Shared Memory SHM (SysV/POSIX)
➢ Message Queues (SysV/POSIX)
➢ Semaphores (SysV/POSIX)
➢ Signals
IPC – process PIPEs

Proc 1


         a = open(PIPE, “r”);
         b = open(PIPE, “w”);


Proc 2


         close(a); close(b);
         a = open(PIPE, “w”);
         b = open(PIPE, “r”);
IPC – Shared Memory

       Proc 1                       SHM

shmid = shmget(IPC_PRIVATE, ...);
mem = shmat(shmid, ...);


        Proc 2


memset(mem, '0', 200);
IPC – Message Queues


                   Proc 1




                       Proc 2
IPC – Semaphores


                   Proc 1




                   Proc 2
IPC – Signals


                Proc 1




                Proc 2
Signals - Kill


                 Proc 1




                 Proc 2
Signals – Signal handlers

Proc 1
                       sig_handler
           main()


         init_some()


         do_some()


         log_some()
Signals – Signal handlers

      Proc 1
                              sig_handler
                  main()


 SIGHUP         init_some()

0 - 255
                do_some()


                log_some()
Processes and Threads
Free Trainings @ Telerik Academy

   C# Programming @ Telerik Academy
    

 Telerik Software Academy

    

 Telerik Academy @ Facebook

    

 Telerik Software Academy Forums

    

6. processes and threads

  • 1.
    6 – Processes and Threads Borislav Varadinov Marian Marinov System CEO of 1H Ltd. Administrator mm@1h.com bobi [ at ] itp.bg
  • 2.
    What is aProcess?
  • 3.
    What is aProcess? ➢ Single process OS – Arduino ➢ Multy process OS – Any modern kernel
  • 4.
    What is aProcess? MEMORY CPU I/O APPLICATION
  • 5.
    Multiple Processes? MEMORY MEMORY CPU I/O APPLICATION APPLICATION
  • 6.
    Multiple Processes? ➢ SegmentationFault ➢ Bus Error ➢ Access violation SEGFAULT/SIGSEGV ➢ Since Linux 3.2 CROSS MEMORY ATTACH
  • 7.
    What is aThread?
  • 8.
    What is aThread? MEMORY CPU I/O Thread0 Thread1 Process
  • 9.
    What is aThread?
  • 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.
    What is aThread?
  • 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("I'm the child(%d)!n", p); } else { for (i=0;i<=3;i++) printf("My child is %dn", p); } return 0; }
  • 13.
    Creating a process hackman@terion:~$./f My child is 3721 My child is 3721 My child is 3721 My child is 3721 I'm the child(0)! I'm the child(0)! I'm the child(0)! I'm the child(0)! hackman@terion:~$
  • 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("I'm 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.
    Creating a process hackman@terion:~$./f My child is 3762 My child is 3762 My child is 3762 My child is 3762 This is not good(3761)! I'm the child(3762)! I'm the child(3762)! I'm the child(3762)! I'm the child(3762)! This is not good(3762)! hackman@terion:~$
  • 16.
    Creating a process EXECfunctions 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.
    Creating a process system()= exec('/bin/sh -c CMD') Which means: your process - exec /bin/sh - exec CMD
  • 18.
    Types of processes ➢Foreground ➢ Background ➢ Daemons ➢ co-relation with terminal
  • 19.
    Foreground processes ➢ Ithas access to the terminal's ➢ STDIN –0 ➢ STDOUT – 1 ➢ STDERR – 2 ➢ It is directly controlled by the user ➢ It is connected to the terminal (text or graphic)
  • 20.
    Background processes ➢ Ithas access only to the terminal's ➢ STDOUT – 1 ➢ STDERR – 2 ➢ It is NOT directly controlled by the user ➢ It is connected to the terminal (text or graphic)
  • 21.
    Daemon processes ➢ ItsSTDIN/OUT/ERR are redirected to files ➢ It is NOT directly controlled by the user ➢ It is NOT connected to the terminal (text or graphic)
  • 22.
    IPC ➢ FS PIPES ➢Process PIPES ➢ Unix Domain Socket ➢ Shared Memory SHM (SysV/POSIX) ➢ Message Queues (SysV/POSIX) ➢ Semaphores (SysV/POSIX) ➢ Signals
  • 23.
    IPC – processPIPEs Proc 1 a = open(PIPE, “r”); b = open(PIPE, “w”); Proc 2 close(a); close(b); a = open(PIPE, “w”); b = open(PIPE, “r”);
  • 24.
    IPC – SharedMemory Proc 1 SHM shmid = shmget(IPC_PRIVATE, ...); mem = shmat(shmid, ...); Proc 2 memset(mem, '0', 200);
  • 25.
    IPC – MessageQueues Proc 1 Proc 2
  • 26.
    IPC – Semaphores Proc 1 Proc 2
  • 27.
    IPC – Signals Proc 1 Proc 2
  • 28.
    Signals - Kill Proc 1 Proc 2
  • 29.
    Signals – Signalhandlers Proc 1 sig_handler main() init_some() do_some() log_some()
  • 30.
    Signals – Signalhandlers Proc 1 sig_handler main() SIGHUP init_some() 0 - 255 do_some() log_some()
  • 31.
  • 32.
    Free Trainings @Telerik Academy  C# Programming @ Telerik Academy   Telerik Software Academy   Telerik Academy @ Facebook   Telerik Software Academy Forums 