Hajdúbagosi Sportegyesület
Hajrá Hajdúbagos!

MENÜ

Alacsony I/O
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>

#define N 100

int main(int argc, char *argv[]){
char T[N];
int x;

    if (argc<3){
    fprintf(stderr,"keves argumentum");
      return 1;
        }

int be = open(argv[1],O_RDONLY);
int ki = open(argv[2],O_WRONLY| O_CREAT| O_TRUNC| S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);

   while((x=read(be,T,N))>0)
      write(ki,T,x);

close(be);
close(ki);
}
--------------------------------------------
Formázott I/O
#include<stdio.h>

int main(){
   int pos;
   char c, text[100];
   FILE *f;

   // Opening file
   f=fopen("file.txt","w+");
   if(f==NULL){
      fprintf(stderr," There is a file opening problem!\n");
      return 1;
      }

   // Write the file
   fprintf(stdout," What word would you like to write into the file?\n   ");
   fscanf(stdin,"%s",text);
   fprintf(f,"%s",text);

   // Positioning and rewriting
   fprintf(stdout," Let's owerwrite something!\n  From which character? Give a number! ");
   fscanf(stdin,"%d",&pos);
   fprintf(stdout,"  What is the new text? ");
   fscanf(stdin,"%s",text);
   fseek(f,pos-1,SEEK_SET);
   fprintf(f,"%s",text);

   // Reading the file
   fprintf(stdout," Now the file contetnt is:\n   ");
   fseek(f,0,SEEK_SET);
   while(1){
      fscanf(f,"%c",&c);
      if(feof(f)) break;
      fprintf(stdout,"%c",c);
      }

   // Closing the file
   fclose(f);

   return 0;
   }
--------------------------------------------------
/*** Listing a directory ***/
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>

int main(){
   DIR *d;
   struct dirent *entry;
   d=opendir(".");
   printf(" -my ls:\n");
   entry=readdir(d);
   while(entry!=NULL){
      if((*entry).d_name[0]!='.')
         printf("%s  ",(*entry).d_name);
      entry=readdir(d);
      }
   closedir(d);

   printf("\n -system ls:\n");
   chdir(".");
   system("ls");

   return 0;
   }
------------------------------------
   /*** Example of inode reading ***/
#include<stdio.h>
#include<sys/stat.h>
#include<time.h>

int main(int argc, char* argv[]){
  struct stat inode;
  if(argc==1) {
    fprintf(stderr," File or directory name is necessary");
    return 1;
    }

  stat(argv[1],&inode);
  printf(" \"%s\" is a %s.\n",argv[1],(inode.st_mode&S_IFDIR)?"directory":"file");
-----------------------------------------------
/*** Using temporary file ***/
#include <stdio.h>

int main(){
   char tmpname[L_tmpnam];
   char text[100];
   char *filename;
   FILE *tmpfp;

   filename = tmpnam(tmpname);
   printf("A temporary file name is: %s\n", tmpname);

   tmpfp = tmpfile();
   if(tmpfp){
      fprintf(tmpfp,"It_is_not_importanat.\n");
      fseek(tmpfp,0L,SEEK_SET);
      fscanf(tmpfp,"%s",text);
      printf("Tempfile contains: %s\n",text);
      fclose(tmpfp);
      }
   else
      printf("Temporary file problem!\n");

   return 0;
   }
-----------------------------------------------
/***  Example of fork  ***/
// gcc -o fork fork.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main(){
   int x=0;
   pid_t pid;
   fprintf(stdout,"Start...  x=%d\n",x);
   pid=fork();
   if(pid==0){  // Child
      x=1;
      fprintf(stderr,"Child...  x=%d (PID=%d, parent is %d)\n",x,getpid(),getppid());
      sleep(2);  // Child is working...
      fprintf(stderr,"Child ps:\n");
      system("ps -al |grep fork >&2");
      sleep(2);
      fprintf(stderr,"End child. (PID=%d)\n",getpid());
      return 0;
      }
   else{  // Parent
      x=2;
      fprintf(stdout,"Parent... x=%d (PID=%d)\n",x,getpid());
      sleep(6);  // Parent is working...
      fprintf(stdout,"Paternt ps:\n");
      system("ps -al |grep fork");
      sleep(2);
      fprintf(stdout,"End parent. (PID=%d)\n",getpid());
      return 0;
      }
   }
------------------------------------------------------------
/*** Signal catch ***/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void jajj(int sig){
   printf("\n Jajj!\n");
   signal(SIGINT, SIG_DFL); // back to default signal handling
   }

int main(){
   int i;

   signal(SIGINT, jajj);

   printf("Start...\n");
   for(i=1;i<=10;i++){
      sleep(1);
      printf(" sleeptime: %d s\n",i);
      }
   return 0;
   }
----------------------------------
/***  Signal sending  ***/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<sys/wait.h>

static int isListen=0;

void listen(int sig){
   isListen=1;
   }

int main(){
   pid_t pid;
   pid=fork();
   if(pid==0){  // child
      printf(" Child: I am working.\n");
      sleep(6); // Child is working
      printf(" Child: I am ready.\n");
      kill(getppid(),SIGUSR1);
      }
   else {  // parent
      printf(" Parent: I am working.\n");
      sleep(3); // Parent is working
      signal(SIGUSR1,listen);
      printf(" Parent: I am waiting for Child's results.\n");
      pause();
      if(isListen==1)
         printf(" Parent: I continue my work.\n");
      else{
         printf(" Parent: I got a signal, but not SIGUSR1.\n");
         return 2;
         }
      sleep(3);
      printf(" The End.\n");
      }
   return 0;
   }
---------------------------------------------------
/***  Synchronizing by SIGUSR1 signal sending  ***/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<sys/wait.h>

static int isListen=0;

void listen(int sig){
   isListen=1;
   }

void nolisten(){
   isListen=0;
   }

int main(){
   pid_t pid;
   pid=fork();
   if(pid==0){  // child
      printf(" Child: I am working.\n");
      sleep(6); // Child is working
      printf(" Child: I am ready.\n");
      kill(getppid(),SIGUSR1);
      sleep(3); // Child is working
      signal(SIGUSR1,listen);
      printf(" Child: I am waiting for Parent.\n");
      pause();
      if(isListen==1)
         printf(" Child: I continue my work.\n");
      else{
         printf(" Child: I got a signal, but not SIGUSR1.\n");
         return 1;
         }
      sleep(6); // Child is working
      printf(" Child: I am ready.\n");
      }
   else {  // parent
      printf(" Parent: I am working.\n");
      sleep(3); // Parent is working
      signal(SIGUSR1,listen);
      printf(" Parent: I am waiting for Child.\n");
      pause();
      if(isListen==1)
         printf(" Parent: I continue my work.\n");
      else{
         printf(" Parent: I got a signal, but not SIGUSR1.\n");
         return 2;
         }
      nolisten();
      sleep(6);  // Parent is working
      printf(" Parent: I am ready.\n");
      kill(pid, SIGUSR1);
      sleep(3);
      printf(" Parent: I am ready.\n");
      wait(NULL);  // Parent wait for Child end
      printf(" The End.\n");
      }
   return 0;
   }
---------------------------------------
/***  Example of alarm function and SIGALRM signal.  ***/
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<unistd.h>

void end(int sig){
   fprintf(stderr,"\n Sorry, time out!\n");
   exit(1);
   }

int main(){
   int a;
   signal(SIGALRM,end);
   alarm(5);
   printf(" Write an integer number within 5 seconds: ");
   scanf("%d",&a);
   printf(" The number is: %d\n",a);
   return 0;
   }
------------------------------------
/*** Signal catch ***/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void jajj(int sig){
   printf("\n Jajj!\n");
   signal(SIGINT, SIG_DFL); // back to default signal handling
   }

int main(){
   int i;

   signal(SIGINT, jajj);

   printf("Start...\n");
   for(i=1;i<=10;i++){
      sleep(1);
      printf(" sleeptime: %d s\n",i);
      }
   return 0;
   }
----------------------------------
*** Pseudo-random numbers ***/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 1000000

int main(){
  int i;
  int x;
  int a[6]={0,0,0,0,0,0};

  srand(time(NULL));
  printf("0<=%d<=%d\n",rand(),RAND_MAX);
  printf("0.000000<=%f<=1.000000\n",(float)rand()/RAND_MAX);
  printf("Dice (%d throw):\n",N);
  for(i=0;i<N;i++){
    do
      x=rand();
    while(x>=RAND_MAX-RAND_MAX%6);  // To ensure equal probability
    a[x%6]++;
    }
  for(i=0;i<6;i++)
    printf("%d:\t%.4f%%\n",i+1,(float)a[i]/N*100);

  return 0;
  }
----------------------------------------------------
int main (int argc, char *argv[]){
    if (argc == 1){
        fprintf (stderr, "Keves argumentum!\n");
    }
    srand (time(NULL));
    struct dirent *entry;
    char T[100], c, T2[100];
    struct stat inode;
    DIR *d;
    FILE *f;
    int random = rand()%57+65;
    printf ("%c\n", random);
    d = opendir(".");
    entry = readdir(d);
    while (entry != NULL){
        if ((*entry).d_name[0] != '.')
            printf ("%s ", (*entry).d_name);
        entry=readdir(d);
    }
    printf ("\n");
    closedir(d);
    fprintf (stdout,"Kerek egy file vagy konyvtar nevet: ");
    fscanf (stdin, "%s", T);
    stat (T, &inode);
    printf ("%s is a %s\n", T, (inode.st_mode&S_IFDIR) ? "Directory" : "File");
    while(1){
        if (inode.st_mode&S_IFDIR){           
            d = opendir (T);
            chdir (T);
            entry = readdir(d);
            while (entry != NULL){
                if ((*entry).d_name[0] != '.')
                    printf ("%s ", (*entry).d_name);
                entry = readdir(d);       
            }
            closedir(d);
            fprintf (stdout, "\nKerek egy nevet: ");
            fscanf (stdin, "%s", T);
            stat(T, &inode);   
            printf ("%s is a %s\n", T, (inode.st_mode&S_IFDIR) ? "Directory" : "File");
            continue;
        }//Ifvége
        else{
            f = fopen (T, "r");
           
            while(1){
                fscanf (f, "%c", &c);
                titkosit (c);
                if (feof(f)) break;
                fprintf (stdout, "%c", c);               
            }           
            break;
        }//Else vége
    }//While vége
}
----------------------------------------------------
//Felkialtojel az utolso karakter helyén alacsony szintu fajlkezelessel
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#define N 1000

int main(int argc, char* argv[]){
    int f = open ("PID.txt", O_RDWR);
    char buffer[N];
    int nread, i;
    while ((nread = read(f, buffer,N))>0)
    close(f);
    for (i = strlen(buffer);i >=0; i--){
        if ((buffer[i] >= 'a' && buffer[i] <='z') || (buffer[i] >= 'A' && buffer[i] <= 'Z')){
            buffer[i] = '!';
            f = open ("PID.txt", O_TRUNC | O_RDWR);
            write (f, buffer, strlen(buffer));
            break;
        }
    }
    close(f);
    return 0;
}//main
--------------------------------------
//8. Írj egy C programot, amiben egy szülonek két gyerek folyamata van és a késobb létrejött gyerek írja ki a korábbi PID-jét! (öccs a bátyó pidjét)
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#define N 1000

int main(int argc, char* argv[]){
    pid_t pid, pid2;
    pid = fork();
    if (pid == 0){
        printf ("Batyj: %d\n", getpid());
        return 0;
    }
    else{
        pid2 = fork();
        if (pid2 == 0){
            printf ("Ocsi Batyj Pid-je: %d\n", pid);
            return 0;
        }
        printf ("Szulo pid: %d\n", getpid());
    }
    return 0;
}//main
----------------------------------------------
//6. Írj egy C programot, amely két részbol áll. A program 1 másodpercenként kiírja az A betut, Ctrl+C hatására pedig a B betut, ugyanúgy 1 másodpercenként.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#define N 1000

static int listen = 0;
void b (int s){
    signal (SIGINT, SIG_DFL);
    while(1){
    printf ("B\n");    
    sleep(1);    
    }
}
int main(int argc, char* argv[]){
    int i;
    signal (SIGINT, b);
    while(1){
        printf ("A\n");
        sleep(1);
    }
    return 0;
}//main
+++++++++++++++++++++++++++++++++++++++++++++
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/stat.h>


void change(int pid){
    FILE *g = fopen("temp.txt","w");
    fprintf(g,"B");
    fclose(g);
    signal(SIGINT,SIG_DFL);
}

int main(){
char c[1024];
int f;
signal(SIGINT,change);
while(1){
    f=open("temp.txt",O_RDONLY);
    read(f,c,1024);
    close(f);
    printf("%s",c);
    sleep(1);
}
return 0;
}
----------------------------------------------
//feladat2: irj egy olyan programot ami letrehoz egy gyerekfolyamatot, a gyerekfolyamat pedig meg egy gyerekfolyamatot, es ez utobbi irja ki a nagyapjanak a pidjet, azaz az unoka irja ki a nagyapja pidjett
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#define N 1000

static int listen = 0;
void islisten(int s){
    listen = 1;
}
void bezar (int s){
    printf ("Bezarom a programot!\n");
    kill(getppid(), SIGKILL);
}
    
int main(int argc, char* argv[]){
    pid_t pid, pid2;
    pid = fork();
    FILE *f;
    int d;
    if (pid == 0){
        signal (SIGUSR1, islisten);
        pause();
        if (listen){}
        pid2 = fork();
        if (pid2 == 0){
            f = fopen ("PID2.txt", "r");
            fscanf (f, "%d",&d);
            fprintf (stdout,"Papa PID-je: %d\n", d);
            fclose(f);
            return 0;
        }
        else{return 0;}
    }
    else{
        f = fopen ("PID2.txt","w+");
        fprintf (f, "%d", getpid());
        fclose(f);
        kill(pid,SIGUSR1);
    }
    return 0;
}//main
-------------------------------------------------------
//Terminalt bezarja megszakitas kiadasakor
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void megszakitas(int pid){
    printf("\nMegszakitas erkezett\n");
    alarm(0);
    kill(getppid(),SIGKILL);
}

void  timeout(int pid){
    printf("Az ido lejart!\n");
    exit(1);
}

int main(int argc, char** argv){
    int i;
    signal(SIGINT, megszakitas);
    signal(SIGALRM, timeout);
    printf("Visszaszamolok 5 masodpercrol!\n");
    alarm(5);
    for(i=5;i>=1;i--) {
        printf("%d. s\n",i);
        sleep(1);
    }
    return 0;
}
-------------------------------------------------------------
//Legkisebb file-t megkeressuk az aktualis konyvtarban majd hozzafuzunk egy szoveget
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>

int main(int argc, char** argv){
int min=0xFFFFFF;
char t[40];
struct dirent *entry;
struct stat inode;
DIR *d = opendir(".");
entry=readdir(d);
while(entry!=NULL){
    stat((*entry).d_name,&inode);
    if((inode.st_mode & S_IFDIR)==0 && strstr((*entry).d_name,".txt\0") && !strstr((*entry).d_name,".txt~\0"))
        if(inode.st_size<=min){
            min=inode.st_size;
            strcpy(t,(*entry).d_name);        
        }
    entry=readdir(d);
}
closedir(d);
FILE *f = fopen(t,"a");
fprintf(f,"\nEzt a szoveget fuzom hozza a dokumentumhoz.\n");
fclose(f);
printf("%s \t %d\n",t,min);
return 0;
}
-----------------------------------------------
//Karakterszámolás alacsony szinten
int main(int argc, char* argv[]){
    int f = open ("PID.txt", O_RDWR);
    int nread,i;
    char buffer[N];
    while ((nread = read (f, buffer, N))>0){
        write (1, buffer, nread);
    }
    for (i= strlen(buffer); i>=0; i--)
        write(1,&(buffer[i]),sizeof(char));
    close (f);    
}//main
----------------------------------------------
//3. Írj egy C programot, amely egy létezo file tartalmát visszafelé a képernyore íratja!
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#define N 1000

static int listen = 0;

    
int main(int argc, char* argv[]){
    FILE *f = fopen ("PID.txt", "r+");
    int i = 0;
    char c, d;
    while (1){
        fscanf (f,"%c", &c);
        i++;
        if (feof(f)) break;
        fprintf (stdout,"%c", c);
    }
    fprintf (stdout,"\ni = %d\n", i);
    fseek (f,i,SEEK_SET);
    while (i>-1){
        fscanf (f, "%c", &d);
        fprintf (stdout, "%c", d);
        i--;
        fseek(f, i, SEEK_SET);
    }
    fprintf (stdout,"\n");
    fclose(f);    
    return 0;
}//main
---------------------------------------

9. Írj egy C programot, amely a parancssori argumentumban megadott létezo fájl nevét
beolvassa és alacsony szintu fájlkezeléssel törli a fájl tartalmát, de magát a fájlt nem!
...
int in = open (argv[1], O_TRUNC|O_RDWR);
    close (in)
....
----------------------------------------

 

Asztali nézet