Advertisement
Guest User

Untitled

a guest
May 15th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <fcntl.h>
  6. #include <dirent.h>
  7. #include <string.h> /* memset */
  8. #include <errno.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11.  
  12.  
  13.  
  14.  
  15. #define BUFSIZE 1024
  16. #define CPC 10
  17. #define NC 10
  18.  
  19. extern char **environ;
  20.  
  21.  
  22. int main(int argc, char **argv, char **envp) {
  23.     int p = 0, p1 = 0, f, n = 5, c, i, j,xd;
  24.     char *b, *n1;
  25.     c = NC;
  26.     n1 = "//home//stud//common//tekst1.txt";
  27.  
  28.     printf("%s[%d]: Poczatek procesu glownego...\n",
  29.         *argv, getpid());
  30.     printf("%s", "Podaj od ktrego bitu mam zaczac czytac:\n");
  31.     scanf("%d", xd);
  32.  
  33.  
  34.  
  35.  
  36.  
  37.     f = open(n1, O_RDONLY);
  38.     for (i = 0; i<n; i++) {
  39.         p = fork();
  40.        
  41.        
  42.        
  43.  
  44.  
  45.  
  46.         if (p == -1)
  47.             printf("%s[%d]: BLAD! Nie moge stworzyc procesu!\n",
  48.                 *argv, getpid());
  49.  
  50.         else if (p == 0) {
  51.             printf("%s: Jestem procesem potomnym [%d] , moj rodzic to: [%d]...\n",
  52.                 *argv, getpid(), getppid());
  53.             sleep(1);
  54.             lseek(f, 20, SEEK_SET);
  55.             b = malloc(sizeof(char)*c + 1);
  56.             j = read(f, b, c);
  57.             b[c + 1] = '\n';
  58.             printf("%s: Przeczytano %d znaków, poczynajac od: %d,  z pliku %s: \"%s\"\n",
  59.                 argv[0], j, 20, n1, b);
  60.             free(b);
  61.             exit(0);
  62.         }
  63.     }
  64.  
  65.     p1 = wait(NULL);
  66.     close(f);
  67.     printf("%s[%d]: Koniec procesu glownego.\n",
  68.         *argv, getpid());
  69.     exit(0);
  70.     return(0);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement