Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/wait.h>
- bool lpierwsza(int liczba){
- for(int i=2;i*i<=liczba;i++){
- if(liczba%i==0)
- return false;
- }
- return true;
- }
- int main()
- {
- char buff[10];
- int rd;
- int i, k;
- int f = fork();
- int file1 = open("tmp1.txt", O_CREAT | O_RDWR | O_TRUNC);
- int file2 = open("tmp2.txt", O_CREAT | O_RDWR | O_TRUNC);
- if(file1 == -1 || file2 == -1)
- {
- printf("Blad podczas odczytu pliku\n");
- return 0;
- }
- if(f > 0){
- i = 1;
- k = 99;
- }
- else{
- i = 100;
- k = 200;
- }
- for(i; i <= k; i++){
- if(lpierwsza(i)){
- sprintf(buff, "%d\n", i);
- if(f > 0){
- write(file1, buff, sizeof(buff));
- }
- else{
- write(file2, buff, sizeof(buff));
- }
- }
- }
- wait(NULL);
- lseek(file1, 0, SEEK_SET);
- lseek(file2, 0, SEEK_SET);
- while((rd = read(file1, buff, sizeof(buff))) > 0){
- printf("%s", buf);
- }
- while((rd = read(file2, buff, sizeof(buff))) > 0){
- printf("%s", buf);
- }
- close(file1);
- close(file2);
- return 0;
- }
Add Comment
Please, Sign In to add comment