Operating Systems 173323 (3 cu)

Week 43

Exercise 2

  1. Confirm how to use ps command to show the processes you have in cs-computer. How to use kill command to delete processes ? Show some examples in exercises.

  2. How to use nice command to change the scheduling priority of the process ? Show an example in cs-computer.

    Save the programs and your solutions of the following tasks to cs-computer so that you can access the files in exercise session.

  3. In the lectures, the following c-program was introduced for creating new child processes. Add comment lines before each command line to explain the program code. Compile the program in cs-computer and execute the program. When program is executing, have a look to PID and PPID of these two processes.
    #include <stdio.h>
    
    main()
    {
    int pid;
         
     pid = fork();
     if (pid < 0) 
       {
        fprintf(stderr,"Fork failed");
        exit(-1);
       }
     if (pid == 0)
       {
          execlp("/usr/local/bin/top","top",NULL);
       }
     else
       { 
         wait(NULL);
         printf("Child complete\n");
         exit(0);
       }
    }
    

  4. Modify the program in the previous task so that it will create two child processes. In the first child process, execute /bin/finger and in the second child process, execute /bin/who. Use while(wait(NULL) != -1); to wait until the both child processes are being executed. Compile the program in cs and execute the program.

  5. Communication between processes in Unix can be done, for example, using pipes (putki). See man pipe and explain in detailed what the following program does. Compile the program in cs and execute the program.
    #include <stdio.h>
    #include <string.h>
    
    main()
    {
      int putki[2], pid, N;
      if (pipe(putki) != 0) {perror("pipe"); exit(1);}
      pid = fork();
      if (pid == -1) {perror("fork"); exit(1);}
      if (pid != 0) {
          char message1[1024];
          close(putki[1]); 
          if (read(putki[0],message1,1024) < 0)
             {perror("read putki"); exit(1);}
          printf("Child: %s\n",message1);
          close(putki[0]);
          printf("Parent: Yes.\n");
      }else{
          char message2[1024];
          close(putki[0]); 
          strcpy(message2,"Can you hear me ?");
          N=strlen(message2);
          if (write(putki[1],message2,N) != N)
             {perror("write putki"); exit(2);}
          close(putki[1]);
      }
      exit(0);
    }