Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- int write_file ()
- {
- FILE *file = fopen( "./testfile.txt", "w" );
- fprintf(file, "testtext\n123456");
- fclose( file );
- return 0;
- }
- int read_file ()
- {
- FILE *file = fopen( "./testfile.txt", "r" );
- int character;
- while ( ( character = fgetc( file ) ) != EOF )
- {
- printf( "%c", character );
- }
- printf("\n");
- fclose( file );
- return 0;
- }
- int lock()
- {
- FILE *lockfile = fopen( "./lock.lck", "w" );
- fclose( lockfile );
- return 0;
- }
- int unlock()
- {
- remove("./lock.lck");
- return 0;
- }
- int exist_file()
- {
- FILE *fp = fopen ( "./lock.lck", "r");
- if (fp == NULL) { // file does not exist
- return 0;
- }
- else{
- return 1;
- }
- }
- int main ( int argc, char *argv[] )
- {
- int i=0;
- pid_t npid; //variable for PID
- npid = fork(); //fork parent process
- //while(1){
- if(npid > 0){ //if parent process
- lock();
- write_file();
- i = exist_file();
- printf("return status = %d", i);
- printf("test");
- unlock();
- i = exist_file();
- }
- else if(npid == 0){ //if child process
- read_file();
- }
- else if(npid == -1){
- printf("ERROR");
- }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement