Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9154339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:24:46+00:00 2026-06-17T12:24:46+00:00

I m working with File Locking in interprocess Communication , the following code bugs

  • 0

I m working with File Locking in interprocess Communication ,
the following code bugs me…
when run through terminal in Macintosh

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <unistd.h>

  int main(int argc , char *argv[])
  {
   // l_type , l_whence , l_start , l_len , l_pid
    struct flock f1 = {F_WRLCK , SEEK_SET , 0 , 0 , 0};
    int fd;

  f1.l_pid = getpid() ;

// if command line arguments  , then assign a Read Lock
if (argc > 1)
{
    f1.l_type = F_RDLCK ;
}

if ((fd = open("lockdemo.c", O_RDWR)) == -1)
{
    perror("open");
    exit(1);
}

printf("Press <RETURN> to try to get lock");
getchar() ;

printf("trying to get lock...");

if (fcntl(fd, F_SETLKW , &f1)  == -1)
{
    perror("fcntl");
    exit(1);
}


printf("got lock !\n");
printf("Press <RETURN> to release lock:");
getchar();

f1.l_type = F_UNLCK ; //set to unlock same region

if (fcntl(fd, F_SETLK , &f1) == -1)
{
    perror("fcntl");
    exit(1);
}

printf("Unlocked .. \n");
close(fd);

return 0;
 }

But the following error appears :
fnctl : invalid argument

plz help me in this question…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:24:47+00:00Added an answer on June 17, 2026 at 12:24 pm

    On MacOs, the members of struct flock have a different order than in Linux.

    to make your code portable you should assign the fields by name, and not assume a particular order.

    From the MacOS Man page for fcntl:

             struct flock {
                 off_t       l_start;    /* starting offset */
                 off_t       l_len;      /* len = 0 means until end of file */
                 pid_t       l_pid;      /* lock owner */
                 short       l_type;     /* lock type: read/write, etc. */
                 short       l_whence;   /* type of l_start */
             };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with Rails 3.1 (rc5), and I'm noticing that any coffeescript file I include
This is some code from a php file im working with. I need to
I am working on some C code and am having a problem with locking
I have a working file rename java tool, now I want to add an
Am working on file / folder event capturing of OS X 10.5 and 10.6
On whatever reason this is not working (says 'file not found'), set in=c:\myprogram\_save cd
This is my first time working with file i/o in java, and it's not
I working on adding file uploading to my web application. I'm using an iframe
I am working on pom file modifications to change the version node value. I
I'm working on a file format that should be written and read in several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.