0

I am having some problems, when trying to write to a file on a server. Different clients can perform read/write requests. I am using fcntl() to lock the file when a write request is made. However, when a process(client) obtains a lock and another process tries to acquire the lock, the contents of the file are deleted. Any idea why this is happening? Below is the code I'm talking about :

            {

            FILE *file = fopen("file.txt", "w+");

            if (file == NULL)
                printf("File cannot be opened");


            fd = fileno(file);

            printf("\nThis is the file descriptor : %d\n", fd);

            if(file == NULL)
            printf("File cannot be opened");

            printf("\nLocking!!!!");

            //initliazing the flock structure
            if(memset(&lock, 0, sizeof(lock))== NULL) //setting 0 as a value
            {
                perror("Memset Error : Should return a pointer to memory area");
            }
            lock.l_type = F_WRLCK;  //F_RDLCK, F_WRLCK, F_UNLCK
            lock.l_whence = SEEK_SET;  //SEEK_SET, SEEK_CUR, SEEK_END
            lock.l_start = 0;   //offset from l_whence
            lock.l_len = 0;   //length, 0 = to EOF
            lock.l_pid = getpid(); //the processes's PID


            //placing a write lock on the file
            if((flags = fcntl(fd, F_SETLKW, &lock)) < 0)
            {
                perror("fnctl: ");
            }
            printf("\nFlag : %d \n", flags);

            printf("\nLocked-------");

            if(fwrite(buff + 1, 1, strlen(buff) - 1, file) == 0)
            {
                perror("Error or EOF reached while fwrite");
            }

            //lock_realease(&l);
            printf("\nHit enter to unlock the file !");
            getchar();

            printf("\nFinished writing so we can unlock file !");

            //Releasing lock
            lock.l_type = F_UNLCK;  //unlocks the region of the file
            if((flags = fcntl(fd, F_SETLKW,&lock)) < 0)
            {
                perror("fnctl: ");
            }

            printf("\nFlag : %d \n", flags);

            fclose(file); //some say it is better to close the file and let it implicitly be unlocked when it is actually closed.
                          //Otherwise the lock can be release while unbuffered data is still unwritten
            printf("\nFile unlocked!");

            }
Yann
  • 318
  • 1
  • 14
  • You erase the file content at the open and you try to fill it with buff but at the present code, buff is uninitialized. Show the wall code to be helped, for the moment I just can say take care of your buffer and your fopen call. – Yann Jun 24 '14 at 14:28
  • Hi Yan, thanks for your help... this is the initialization of the buffer and the call for write at the client side { buff = "Systems Programming Assignments "; ssize_t bytes_written = mwrite(address, 0, buff, strlen(buff)); mapped_location = (fileloc_t *)address; printf("Updated Memory Content:%s\n\n", mapped_location->pathname); } – user3770009 Jun 24 '14 at 14:40
  • __"fwrite(buff + 1" may be the problem__ try with __"fwrite(buff"__, and in "fwrite(buff + 1, 1, strlen(buff) - 1, file) == 0)" replace the second argument =>"1" by a "sizeof(char)" it should be better. – Yann Jun 24 '14 at 14:56

0 Answers0