Docstoc

thread,process,program

Document Sample
thread,process,program Powered By Docstoc
					Program Process Thread
Process
❑ Process structure, type, and scheduling
❑ Starting new processes in different
 ways
❑ Parent, child, and zombie processes
❑ What signals are and how to use them
How a Program Becomes a
Process
 Execution of a program is process
 A Process is a n instance of a Program
     OS copies executable module into a
      program image in main memory
     The process must have
     ○ PID
     ○ Flow of control (thread)
     ○ Memory area
     ○ Other resources
Thread and Thread Execution
 P1 calls P2
 P1
     Loop (222, 223, 224, 225)
   P2
     10, 11, 12
 2221, 2231, 102, 112, 122, 2241, 2251,
  2221, 2231, 102, 112, 122
 Context switching
Layout of a Program Image
Process
   The UNIX standards, specifically IEEE
    Std 1003.1, 2004 Edition, defines a
    process as

    “an address space with one or more
    threads executing within that address
       space, and the required system
        resources for those threads.”
Process
 Linux lets many programs run at once.
 As a multiuser system, Linux allows many
  users to access the system at the same
  time
 a program — or process — that is running
  consists of
       program code,
       data,
       variables (occupying system memory),
       open files (file descriptors),
       and an environment.
Process Structure
Process Structure
The Process Table
   The Linux process table is like a data structure
   Describes all of the processes that are currently
    loaded
     their PID,
     status,
     command string,

 The table is of limited size, so the number of
  processes a system will support is limited.
 Early UNIX systems were limited to 256 processes.
 More modern implementations have relaxed this
  restriction considerably and may be limited only by
  the memory vailable to construct a process table
  entry.
Viewing Processes
Viewing Processes


   The initial login was performed on virtual
    console number 4. This is just the
    console on this machine. The shell
    program that is running is the Linux
    default, bash.
Viewing Processes


   The X Window System was started by
    the command startx. This is a shell
    script that starts the X server and runs
    some initial X programs.
Viewing Processes


   This process represents a window in X
    running Emacs. It was started by the
    window manager in response to a
    request for a new window. A new
    pseudo terminal, pts/0, has been
    assigned for the shell to read from and
    write to.
Process State
Process Scheduling
 nice
 renice




 $ renice 10 1362
 1362: old priority 0, new priority 10
Process Scheduling
Process Scheduling
   The status column now also contains N
    to indicate that the nice value has
    changed from the default.
Starting New Processes

 system
 exec
 fork
Starting New Processes

#include <stdlib.h>
int system (const char *string);

The command is executed as if the
  command is a shell comand
$ sh -c string
System
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf(“Running ps with system\n”);
system(“ps ax”);
printf(“Done.\n”);
exit(0);
}
Output
$ ./system1
Running ps with system
PID TTY STAT TIME COMMAND
1 ? Ss 0:03 init [5]
...
1262 pts/1 Ss 0:00 /bin/bash
1273 pts/2 S 0:00 su -
1274 pts/2 S+ 0:00 -bash
1463 pts/2 SN 0:00 oclock
1465 pts/1 S 0:01 emacs Makefile
1480 pts/1 S+ 0:00 ./system1
1481 pts/1 R+ 0:00 ps ax
Done.
System
   system(“ps ax &“);

$ ./system2
Running ps with system
PID TTY STAT TIME COMMAND
1 ? S 0:03 init [5]
...
Done.
$ 1274 pts/2 S+ 0:00 -bash
1463 pts/1 SN 0:00 oclock
1465 pts/1 S 0:01 emacs Makefile
1484 pts/1 R 0:00 ps ax
Replacing a Process Image
#include <unistd.h>
char **environ;
int execl(const char *path, const char *arg0, ...,
   (char *)0);
int execlp(const char *file, const char *arg0, ...,
   (char *)0);
int execle(const char *path, const char *arg0, ...,
   (char *)0, char *const
envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *path, char *const argv[], char
   *const envp[]);
exec_ _
#include <unistd.h>
                                             /* Example of an argument list */
                          /* Note that we need a program name for argv[0] */

char *const ps_argv[] = {“ps”, “ax”, 0};
                                 /* Example environment, not terribly useful */

char *const ps_envp[] = {“PATH=/bin:/usr/bin”, “TERM=console”, 0};
                                        /* Possible calls to exec functions */

execl(“/bin/ps”, “ps”, “ax”, 0); /* assumes ps is in /bin */
execlp(“ps”, “ps”, “ax”, 0); /* assumes /bin is in PATH */
execle(“/bin/ps”, “ps”, “ax”, 0, ps_envp); /* passes own environment */
execv(“/bin/ps”, ps_argv);
execvp(“ps”, ps_argv);
execve(“/bin/ps”, ps_argv, ps_envp);
execlp
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf(“Running ps with execlp\n”);
execlp(“ps”, “ps”, “ax”, 0);
printf(“Done.\n”);
exit(0);
}
Output
$ ./pexec
Running ps with execlp
PID TTY STAT TIME COMMAND
1 ? S 0:03 init [5]
...
1262 pts/1 Ss 0:00 /bin/bash
1273 pts/2 S 0:00 su -
1274 pts/2 S+ 0:00 -bash
1463 pts/1 SN 0:00 oclock
1465 pts/1 S 0:01 emacs Makefile
1514 pts/1 R+ 0:00 ps ax
Duplicating a Process Image
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
fork
fork
pid_t new_pid;
new_pid = fork();
switch(new_pid) {
case -1 : /* Error */
break;
case 0 : /* We are child */
break;
default : /* We are parent */
break;
}
fork
#include <sys/types.h>               case 0:
#include <unistd.h>                  message = “This is the child”;
#include <stdio.h>                   n = 5;
#include <stdlib.h>
int main()                           break;
{                                    default:
pid_t pid;                           message = “This is the parent”;
char *message;                       n = 3;
int n;                               break;
printf(“fork program starting\n”);
                                     }
pid = fork();
switch(pid)                          for(; n > 0; n--) {
{                                    puts(message);
case -1:                             sleep(1);
perror(“fork failed”);               }
exit(1);                             exit(0);
                                     }
Output
$ ./fork1
fork program starting
This is the child
This is the parent
This is the parent
This is the child
This is the parent
This is the child
This is the child
This is the child
Waiting for a Process
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *stat_loc);
Wait Macroes
wait
 #include <sys/types.h>               case -1:
 #include <sys/wait.h>                perror(“fork failed”);
 #include <unistd.h>                  exit(1);
 #include <stdio.h>                   case 0:
                                      message = “This is the child”;
 #include <stdlib.h>
                                      n = 5;
 int main()
                                      exit_code = 37;
 {
                                      break;
 pid_t pid;
                                      default:
 char *message;                       message = “This is the parent”;
 int n;                               n = 3;
 int exit_code;                       exit_code = 0;
 printf(“fork program starting\n”);   break;
 pid = fork();                        }
 switch(pid)                          for(; n > 0; n--) {
 {                                    puts(message);
                                      sleep(1);
                                      }
wait
if (pid != 0) {
int stat_val;
pid_t child_pid;
child_pid = wait(&stat_val);
printf(“Child has finished: PID = %d\n”, child_pid);
if(WIFEXITED(stat_val))
   printf(“Child exited with code %d\n”, EXITSTATUS(stat_val));
else
   printf(“Child terminated abnormally\n”);
}
exit(exit_code);
}
Output
$ ./wait
fork program starting
This is the child
This is the parent
This is the parent
This is the child
This is the parent
This is the child
This is the child
This is the child
Child has finished: PID = 1582
Child exited with code 37
$
Zombie Processes
switch(pid)
{
case -1:
perror(“fork failed”);
exit(1);
case 0:
message = “This is the child”;
n = 3;
break;
default:
message = “This is the parent”;
n = 5;
break;
}
Zombie Processes
waitpid
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *stat_loc, int
  options);

waitpid(child_pid, (int *) 0, WNOHANG);
I/O Redirection
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
int ch;
while((ch = getchar()) != EOF) {
putchar(toupper(ch));
}
exit(0);
}
Output
$ ./upper
hello there
HELLO THERE
$
Shell Redirection
$ cat file.txt
this is the file, file.txt, it is all lower case.
$ ./upper < file.txt
THIS IS THE FILE, FILE.TXT, IT IS ALL
   LOWER CASE.
Shell Redirection
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *filename;
if (argc != 2) {
fprintf(stderr, “usage: useupper file\n”);
exit(1);
}
filename = argv[1];
Shell Redirection
if(!freopen(filename, “r”, stdin))
{
     fprintf(stderr, “could not redirect stdin from file
    %s\n”, filename);
     exit(2);
}
execl(“./upper”, “upper”, 0);

perror(“could not exec ./upper”);
exit(3);
}
Output
$ ./useupper file.txt
THIS IS THE FILE, FILE.TXT, IT IS ALL
   LOWER CASE.
    Threads vs. Processes

Creation of a new process using fork is
  expensive (time & memory).

A thread (sometimes called a lightweight
  process) does not require lots of memory
  or startup time.




                  Systems Programming        47
            fork()

               fork()
Process A

 Global
                                  Process B
Variables

                                   Global
  Code                            Variables


                                    Code
  Stack


                                    Stack


            Systems Programming               48
          pthread_create()

Process A
 Thread 1

              pthread_create()
 Global
Variables


  Code                               Process A
                                     Thread 2


  Stack
                                       Stack



               Systems Programming               49
Multiple Threads

Each process can include many threads.

All threads of a process share:
   memory (program code and global data)
   open file/socket descriptors
   signal handlers and signal dispositions
   working environment (current directory, user ID,
    etc.)



                     Systems Programming               50

				
DOCUMENT INFO
Description: These slides were prepared by me.And threads and process and program topics has been discussed in these slides.Very beneficial..
Ali butt Ali butt Ali butt http://
About