Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS431-cotter1 Linux Programming, Processes, and Threads Man pages (fork, clone, execve, pthread_create) The Linux Programming Interface – Kerrisk, No Starch,

Similar presentations


Presentation on theme: "CS431-cotter1 Linux Programming, Processes, and Threads Man pages (fork, clone, execve, pthread_create) The Linux Programming Interface – Kerrisk, No Starch,"— Presentation transcript:

1 CS431-cotter1 Linux Programming, Processes, and Threads Man pages (fork, clone, execve, pthread_create) The Linux Programming Interface – Kerrisk, No Starch, 2010 Interprocess Communications in Linux - Gray, Prentice Hall, 2003 Understanding the Linux Kernel - Bovet, Cesati, O-Reilly, 2006

2 CS431-cotter2 Linux Programming API Available as command line or GUI (X windows) –Programming will all be command line, but tools may be either command line or GUI Information sources –manual pages man cc (provides information on using cc compiler and linker) –Information pages info cc (provides linked information. Hypertext)

3 CS431-cotter3 Linux Programming Creating a source file (*.c or *.cpp) –Use any text editor (emacs, vi, or any GUI editor (kwrite))

4 CS431-cotter4 Linux Programming Creating a source file (*.c or *.cpp) –Use any text editor (emacs, vi, or any GUI editor (kwrite)) Compile and Link a program (C language) –cc myprog.c(produces executable file a.out) –gcc myprog.c (produces executable file a.out) –gcc -o myprog myprog.c (produces executable myprog) –gcc -lm -o myprog myprog.c (includes math library and produces executable file myprog)

5 CS431-cotter5 Linux Programming Compile and Link a program (C++ language) –g++ myprog.cpp (produces executable a.out) –g++ -o mynuprog myprog.cpp (produces executable file mynuprog) –g++ -lm -o mynuprog myprog.cpp extra1.cpp extra2.cpp (compiles myprog.cpp, extra1.cpp, extra2.cpp, then links all files together, along with math library, to produce executable mynuprog)

6 CS431-cotter6 Linux Programming Compile and Link a program (C++ language) –g++ myprog.cpp (produces executable a.out) –g++ -o mynuprog myprog.cpp (produces executable file mynuprog) –g++ -lm -o mynuprog myprog.cpp extra1.cpp extra2.cpp (compiles myprog.cpp, extra1.cpp, extra2.cpp, then links all files together, along with math library, to produce executable mynuprog) Run an executable program –./mynuprog

7 CS431-cotter7 Concurrent Processing In Linux Fork () –To create a duplicate version of the parent process Exec () –To replace an existing process context with a different program Pthread_create () –To create a new thread ( POSIX compliant) within an existing process context

8 CS431-cotter8 Creating Processes - fork( ) pid_t fork(void); Creates a child process that differs from the process only in its PID and PPID, and in the fact that resource utilizations are set to 0. Linux uses a copy-on-write procedure. Child process returns PID = 0; Parent process returns child PID

9 CS431-cotter9 Process Fork example #include #define LB_SIZE 128 int main () { time_t now; char lineBuf[LB_SIZE]; pid_t pid; void work (char *name); time (&now); printf ("Fork test at %s\n", ctime(&now));

10 CS431-cotter10 Process Fork example if ((pid = fork() ) < 0) { printf("Couldn't fork process!\n"); exit(1); } else if (pid == 0) { // child process work ("child"); return 0; } work("main"); printf ("Closing main program...\n"); return 0; } void work(char * name) { printf("This is the %s work cycle\n",name); sleep (1); }

11 CS431-cotter11 Process Fork example (Output) [root]@bobs-RHLINUX cs431_linux]# gcc -o proc_fork proctest_v2a.c [root]@bobs-RHLINUX cs431_linux]#./proc_fork Fork test at Wed Jan 10 14:57:53 2001 This is the main work cycle Closing main program... This is the child work cycle

12 CS431-cotter12 Creating Processes - execve( ) int execve ( const char *filename, char *const argv[], char *const envp[]); filename: name of executable file (or script) to be executed argv[]: list of arguments to be passed to executable file (an array of strings) envp[]: list of environmental variables. (may use NULL). No return on success. -1 on failure

13 CS431-cotter13 Creating Processes - Exec Used to call an executable program from a child process. 6 varieties available, depending on options needed:

14 CS431-cotter14 New Process Example (exec.cpp) #include extern char **environ; extern int errno; pid_t pid; using namespace std; int main (int argc, char* argv[]) { FILE *fin; char lineBuf[128]; char *progname= argv[1]; char myname[15] = "Bob"; char mynum[5] = "5"; static int result; char *args[4];

15 CS431-cotter15 New Process Example if (argc == 2) strcpy (progname, argv[1]); else{ cout << "Usage: " << argv[0] << " progname" << endl; exit(1); } pid = fork(); if (pid==0){ //We are in the child process args[0] = progname; args[1] = (char *) &myname; args[2] = (char *) &mynum; args[3] = (char *) 0; // array must be null terminated if ( execv (progname, args) == -1) { cout << "ERROR IN EXECVE " << endl; perror("execve"); }

16 CS431-cotter16 New Process Example else //We are in the parent process { cout << “Created new process with process ID " << pid << endl; sleep(1); } return 0; }//main

17 CS431-cotter17 Child Process (hello.cpp) #include using namespace std; int main(int argc, char * argv[]) { int i, mynum; if (argc != 3) { cout << "Usage: : " << argv[0] << " NAME NUMBER" << endl; return 0; } cout << "Hello, World!" << endl; cout << “Passed in " << argc << " arguments" << endl; cout << "They are: " << endl; mynum = atoi(argv[2]); cout << "Program name is " << argv[0] << endl; cout << "Name argument is " << argv[1] << endl; cout << "The number is " << mynum << endl; return 0; }

18 CS431-cotter18 New Process Example (Output) [rcotter@kc-sce-450p2 cs431]$ g++ -o exec exec.cpp [rcotter@kc-sce-450p2 cs431]$ g++ -o hello hello.cpp [rcotter@kc-sce-450p2 cs431]$./exec hello Created new process with process ID 2795 Hello, World! Passed in 3 arguments They are: Program name is hello Name argument is Bob The number is 5 [rcotter@kc-sce-450p2 cs431]$

19 CS431-cotter19 Creating Threads - _pthread_create() #include int pthread_create ( pthread_t *thread, //variable to store returned thread ID pthread_attr_t *attr, // thread attributes void * (*start_routine)(void *), //function to run void * arg); //args to pass to new thread

20 CS431-cotter20 _pthread_create( ) attributes Thread Attributes –detachstate //control whether the thread is created in the joinable state (default) or not. –schedpolicy //select scheduling policy (default is regular, non-realtime scheduling) –schedparam //set scheduling priority (default is 0) –inheritsched //is sched policy to be inherited by child processes or threads? (default is no) Should be set to NULL to use default attributes May be modified using: –pthread_attr_init, etc.

21 CS431-cotter21 POSIX Thread example #include #define LB_SIZE 128 using namespace std; void * work (void * arg); // function prototype int main (int argc, char * argv[]) { time_t now; pthread_t tid[3]; int childno, childcount = 3; int index, i, childid[3]; void * ptReturn; // a pointer to the return value from a thread time (&now); cout << "Thread Test at " << ctime(&now) << endl;

22 CS431-cotter22 POSIX Thread example for (index = 0; index < childcount; index++){ childid[index] = index; if (( pthread_create(&tid[index], NULL, &work, (void *)&childid[index] )) > 0) { cout << "Couldn't create new thread!" << endl; exit(1); } else //we're in main { cout << "Thread " << index << " has tid " << tid[index] << endl; } } cout << "Now, wait for threads to finish..." << endl;

23 CS431-cotter23 POSIX Thread example for (i = 0; i < childcount; i++){ pthread_join (tid[i], &ptReturn); cout << "Return value for " << tid[i] << " is: " << (long) ptReturn << endl; } cout << "Terminating Main program......." << endl;return 0; } void * work(void * arg) { int i, childnum; childnum = *(int *) arg; //recast as int pointer, then dereference for (i = 0; i < 3; i++){ cout << "Loop " << i << " of thread " << childnum << " work cycle" << endl; sleep (1); } return ((void *)childnum); //Could use pthread_exit ((void *) childnum); }

24 CS431-cotter24 POSIX Thread example (Output) [rcotter@kc-sce-450p2 cs431]$ g++ -o pthread2 pthread_v2.cpp -lpthread [rcotter@kc-sce-450p2 cs431]$./pthread2 Thread Test at Fri Oct 15 10:02:12 2010 Thread Loop 00 has tid of thread 0 work cycle 3077553008 Thread 1 has tid 3067063152 Loop 0 of thread 1 work cycle Thread 2 has tid 3056573296 Now, wait for threads to finish... Loop 0 of thread 2 work cycle Loop 1 of thread 0 work cycle Loop 1 of thread 1 work cycle Loop 1 of thread 2 work cycle Loop 2 of thread 0 work cycle Loop 2 of thread 1 work cycle Loop 2 of thread 2 work cycle Return value for 3077553008 is: 0 Return value for 3067063152 is: 1 Return value for 3056573296 is: 2 Terminating Main program....... [rcotter@kc-sce-450p2 cs431]$

25 CS431-cotter25 Linux Program Debugging Add debugging information to executable program –g++ -ggdb -lm -o myprog myprog.cpp

26 CS431-cotter26 Linux Program Debugging Add debugging information to executable program –g++ -ggdb -lm -o myprog myprog.cpp Generate a copy of source file with line numbers –cat -n myprog.cpp | lpr (concatenate [list] the program with line numbers and send the result to the line printer program, so that it can be printed on the default printer)

27 CS431-cotter27 Linux Program Debugging Add debugging information to executable program –g++ -ggdb -lm -o myprog myprog.cpp Generate a copy of source file with line numbers –cat -n myprog.cpp | lpr (concatenate [list] the program with line numbers and send the result to the line printer program, so that it can be printed on the default printer) Start the Gnu debugger –gdb myprog

28 CS431-cotter28 Debugging Commands Set a breakpoint just before executing line 34 –break 34

29 CS431-cotter29 Debugging Commands Set a breakpoint just before executing line 34 –break 34 Start running the program inside the debugger (with any optional command line arguments) –run test.dat –(program will run to the first break point, until it fails, or until it completes)

30 CS431-cotter30 Debugging Commands Set a breakpoint just before executing line 34 –break 34 Start running the program inside the debugger (with any optional command line arguments) –run test.dat –(program will run to the first break point, until it fails, or until it completes) Show the current value of a program variable –print filename

31 CS431-cotter31 Debugging Commands Single step execution to the next line in the source code. –Next

32 CS431-cotter32 Debugging Commands Single step execution to the next line in the source code. –Next Show the current value of a program variable every time the execution stops –display filename

33 CS431-cotter33 Debugging Commands Single step execution to the next line in the source code. –Next Show the current value of a program variable every time the execution stops –display filename Stop execution of a program running under gdb –kill

34 CS431-cotter34 Debugging Commands Single step execution to the next line in the source code. –Next Show the current value of a program variable every time the execution stops –display filename Stop execution of a program running under gdb –kill Terminate gdb –quit

35 CS431-cotter35 Sample debugging session (source file: myprog.cpp) 14#include 15#include 16#include 17#include 18#include 19 20//using namespace std; 21 22int main (int argc, char *argv[]) 23{ 24char buf[30]; 25char filename[30]; 26time_t now; 27ifstream inFile; 28 29 if (argc != 2) 30{ 31cout << "Usage: " << argv[0] << " input_filename" << endl; 32exit (0); 33}

36 CS431-cotter36 Sample debugging session (source file: myprog.cpp) 14#include 15#include 16#include 17#include 18#include 19 20//using namespace std; 21 22int main (int argc, char *argv[]) 23{ 24char buf[30]; 25char filename[30]; 26time_t now; 27ifstream inFile; 28 29 if (argc != 2) 30{ 31cout << "Usage: " << argv[0] << " input_filename" << endl; 32exit (0); 33}

37 CS431-cotter37 Sample debugging session (source file : myprog.cpp) 34strcpy (filename, argv[1]); 35inFile.open(filename, ios::in, 664); 36if (!inFile.is_open()) 37{ 38cout << "We couldn't open " << filename << endl; 39return (1); 40} 41time (&now); 42cout << "It is now " << ctime (&now) << endl; 43while (inFile.getline (buf, 30)) 44{ 45cout << buf << endl; 46} 47inFile.close(); 48return 0; 49}

38 CS431-cotter38 Sample debugging session (source file : myprog.cpp) 34strcpy (filename, argv[1]); 35inFile.open(filename, ios::in, 664); 36if (!inFile.is_open()) 37{ 38cout << "We couldn't open " << filename << endl; 39return (1); 40} 41time (&now); 42cout << "It is now " << ctime (&now) << endl; 43while (inFile.getline (buf, 30)) 44{ 45cout << buf << endl; 46} 47inFile.close(); 48return 0; 49}

39 CS431-cotter39 Sample debugging session [root@bobs-RHLINUX cs431_linux]# g++ -ggdb -o mpcpp_db myprog.cpp [root@bobs-RHLINUX cs431_linux]# gdb mpcpp_db (gdb) break 34 Breakpoint 1 at 0x8048970: file myprog.cpp, line 34. (gdb) run test.dat Starting program: /home/rcotter/cs431_linux/mpcpp_db test.dat Breakpoint 1, main (argc=2, argv=0xbffffb74) at myprog.cpp:34 34 strcpy (filename, argv[1]); (gdb) print filename $1 = "tûÿ¿Ð>\001@°\203\004\bÈ\234\004\b.\205\004\bð\034\002@(ûÿ¿p©” (gdb) next 35 inFile.open(filename, ios::in, 664); (gdb) print filename $2 = "test.dat\000\203\004\bÈ\234\004\b.\205\004\bð\034\002@(ûÿ¿p©” (gdb) next 36 if (!inFile.is_open()) (gdb) [Just hitting return will repeat the previous command] 41 time (&now);

40 CS431-cotter40 Sample debugging session [root@bobs-RHLINUX cs431_linux]# g++ -ggdb -o mpcpp_db myprog.cpp [root@bobs-RHLINUX cs431_linux]# gdb mpcpp_db (gdb) break 34 Breakpoint 1 at 0x8048970: file myprog.cpp, line 34. (gdb) run test.dat Starting program: /home/rcotter/cs431_linux/mpcpp_db test.dat Breakpoint 1, main (argc=2, argv=0xbffffb74) at myprog.cpp:34 34 strcpy (filename, argv[1]); (gdb) print filename $1 = "tûÿ¿Ð>\001@°\203\004\bÈ\234\004\b.\205\004\bð\034\002@(ûÿ¿p©” (gdb) next 35 inFile.open(filename, ios::in, 664); (gdb) print filename $2 = "test.dat\000\203\004\bÈ\234\004\b.\205\004\bð\034\002@(ûÿ¿p©” (gdb) next 36 if (!inFile.is_open()) (gdb) [Just hitting return will repeat the previous command] 41 time (&now);

41 CS431-cotter41 Sample debugging session [root@bobs-RHLINUX cs431_linux]# g++ -ggdb -o mpcpp_db myprog.cpp [root@bobs-RHLINUX cs431_linux]# gdb mpcpp_db (gdb) break 34 Breakpoint 1 at 0x8048970: file myprog.cpp, line 34. (gdb) run test.dat Starting program: /home/rcotter/cs431_linux/mpcpp_db test.dat Breakpoint 1, main (argc=2, argv=0xbffffb74) at myprog.cpp:34 34 strcpy (filename, argv[1]); (gdb) print filename $1 = "tûÿ¿Ð>\001@°\203\004\bÈ\234\004\b.\205\004\bð\034\002¿p©” (gdb) next 35 inFile.open(filename, ios::in, 664); (gdb) print filename $2 = "test.dat\000\203\004\bÈ\234\004\b.\205\004\bð\034\002¿p©” (gdb) next 36 if (!inFile.is_open()) (gdb)[Just hitting return will repeat the previous command] 41 time (&now);

42 CS431-cotter42 Sample debugging session (gdb) next 42 cout << "It is now " << ctime (&now) << endl; (gdb) It is now Wed Nov 22 15:03:37 2000 43 while (inFile.getline (buf, 30)) (gdb) display buf 1: buf = "û¬\004@x\234\004\b`®\000@tûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) next 45 cout << buf << endl; 1: buf = "/", '*', "\000ûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) /*********** 43 while (inFile.getline (buf, 30)) 1: buf = "/", '*', "\000ûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) 45 cout << buf << endl; 1: buf = "*\tThis is a test\000ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) etc.....

43 CS431-cotter43 Sample debugging session (gdb) next 42 cout << "It is now " << ctime (&now) << endl; (gdb) It is now Wed Nov 22 15:03:37 2000 43 while (inFile.getline (buf, 30)) (gdb) display buf 1: buf = "û¬\004@x\234\004\b`®\000@tûÿ¿(ûÿ¿ë\210\004\bÄ\233\\bx\234” (gdb) next 45 cout << buf << endl; 1: buf = "/", '*', "\000ûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) /*********** 43 while (inFile.getline (buf, 30)) 1: buf = "/", '*', "\000ûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) 45 cout << buf << endl; 1: buf = "*\tThis is a test\000ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) etc.....

44 CS431-cotter44 Sample debugging session (gdb) next 42 cout << "It is now " << ctime (&now) << endl; (gdb) It is now Wed Nov 22 15:03:37 2000 43 while (inFile.getline (buf, 30)) (gdb) display buf 1: buf = "û¬\004@x\234\004\b`®\000@tûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) next 45 cout << buf << endl; 1: buf = "/", '*', "\000ûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) /*********** 43 while (inFile.getline (buf, 30)) 1: buf = "/", '*', "\000ûÿ¿(ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) 45 cout << buf << endl; 1: buf = "*\tThis is a test\000ûÿ¿ë\210\004\bÄ\233\004\bx\234” (gdb) etc.....

45 CS431-cotter45 Summary Linux Processes / Threads –fork( ); –execve( … ); –pthread_create( … );


Download ppt "CS431-cotter1 Linux Programming, Processes, and Threads Man pages (fork, clone, execve, pthread_create) The Linux Programming Interface – Kerrisk, No Starch,"

Similar presentations


Ads by Google