Linux semaphore PV operation

Source: Internet
Author: User
Tags terminates
In the semop operation of Linux semaphore PV, the sem_flg members of the sembuf structure can be 0, IPC_NOWAIT, and SEM_UNDO. When it is set to SEM_UNDO, it will enable the operating system to track the changes made by the current process to this semaphore. if this process stops without releasing this semaphore, operate...

 

Linux semaphore PV operation

In the semop operation, the sem_flg members of the sembuf structure can be 0, IPC_NOWAIT, and SEM_UNDO. When it is set to SEM_UNDO, it enables the operating system to track the changes made by the current process to the Semaphore. if the process terminates without releasing the semaphore, the operating system will automatically release

In the semop operation, the sem_flg members of the sembuf structure can be 0, IPC_NOWAIT, and SEM_UNDO. When it is set to SEM_UNDO, it enables the operating system to track the changes made by the current process to the Semaphore. if the process terminates without releasing the semaphore, the operating system automatically releases the Semaphore held by the process. Unless you have special requirements on semaphores, you should develop a good habit of setting sem_flg as SEM_UNDO.

1: // assume that two processes (parent and child processes) write a file, but only one process can write the file at a time.

2: // use semaphores to perform pv operations

3: # include

4: # include

5: # include

6: # include

7: # include

8: # include

9: struct sembuf sops;

10: static int sid;

11: // create a new semaphore set

12: int createSemset (void)

13 :{

14: char * pathname = "semset ";

15: if (access (pathname, F_ OK )! = 0)

16 :{

17: int fd = open (pathname, O_RDWR | O_CREAT, 0666 );

18: if (fd <0)

19 :{

20: perror ("open ");

21: return-1;

22 :}

23 :}

24: key_t key = ftok (pathname, 'A ');

25: if (-1 = key)

26 :{

27: perror ("ftok ");

28: return-1;

29 :}

30: return semget (key, 1, IPC_CREAT | 0666 );

31 :}

32:

33: // P operation

34: int P (void)

35 :{

36: sops. sem_num = 0;

37: sops. sem_op =-1;

38: sops. sem_flg = 0;

39: return semop (sid, & sops, 1 );

40 :}

41: // V operation

42: int V (void)

43 :{

44: sops. sem_num = 0;

45: sops. sem_op = 1;

46: sops. sem_flg = 0;

47: return semop (sid, & sops, 1 );

48 :}

49: int main (int argc, char * argv [])

50 :{

51: sid = createSemset ();

52: if (-1 = sid)

53 :{

54: perror ("createSemset ");

55: exit (1 );

56 :}

57:

58: if (-1 = semctl (sid, 0, SETVAL, 1 ))

59 :{

60: perror ("SETVAL ");

61: exit (1 );

62 :}

63: pid_t pid = fork ();

64: if (pid <0)

65 :{

66: perror ("fork ");

67: exit (1 );

68 :}

69: else if (0 = pid)

70 :{

71: while (1)

72 :{

73: if (-1 = P ())

74 :{

75: printf ("P operation failed!

");

76: exit (1 );

77 :}

78: printf ("The sub-process is writing the file!

");

79: sleep (1 );

80: printf ("The sub-process write operation is complete, and resources are released!

");

81: if (-1 = V ())

82 :{

83: printf ("V operation failed! ");

84: exit (1 );

85 :}

86 :}

87 :}

88: else

89 :{

90: while (1)

91 :{

92: if (-1 = P ())

93 :{

94: printf ("P operation failed!

");

95: exit (1 );

96 :}

97: printf ("the parent process is writing the file!

");

98: sleep (1 );

99: printf ("the parent process write operation is complete, and resources are released!

");

100: if (-1 = V ())

101 :{

102: printf ("V operation failed! ");

103: exit (1 );

104 :}......

1: void P (int semid)

2 :{

3: struct sembuf sem_p;

4: sem_p.sem_num = 0;

5: sem_p.sem_op =-1;

6: sem_p.sem_flg = SEM_UNDO;

7: if (semop (semid, & sem_p, 1) =-1 ){

8: perror ("p op failed

");

9: exit (1 );

10 :}

11 :}

12:

13: void V (int semid)

14 :{

15: struct sembuf sem_p;

16: sem_p.sem_num = 0;

17: sem_p.sem_op = 1;

18: sem_p.sem_flg = SEM_UNDO;

19: if (semop (semid, & sem_p, 1) =-1 ){

20: perror ("v op failed

");

21: exit (1 );

22 :}

23 :}

PV primitive processes the synchronization and mutex between processes through the operation semaphores. Its core is an inseparable and uninterrupted program.

 

The concept of semaphore was proposed by the famous Dutch computer scientist Dijkstra in 1965. The basic idea is to use a new variable type (semaphore) to record the number of currently available resources. There are two implementation methods: 1) semaphore must be greater than or equal to 0. 0 indicates that there are no idle resources, while a positive number indicates the number of idle resources. 2) the value of semaphore can be positive or negative. the absolute value of a negative number indicates the number of processes waiting to enter the critical section.

 

Semaphores are maintained by the operating system. user processes can only be accessed through initialization and two standard primitives (P and V. You can specify a non-negative integer for initialization, that is, the total number of idle resources.

 

P primitive: P is the first letter of the Dutch Proberen (test. Is the blocking primitive, responsible for converting the current process from the running state to the blocking state until another process wakes up. The operation is as follows: apply for an idle resource (subtract 1 from the semaphore). If it succeeds, it exits. if it fails, the process is blocked;

 

V primitive: V is the first letter of Dutch Verhogen (add. The wake-up primitive is used to wake up a blocked process. it has a parameter table that stores information about the process waiting to be awakened. The operation is to release a occupied resource (add the semaphore to 1). If a blocked process is found, select a wake-up process.

 

The operations on semaphores by PV primitives can be divided into three types:

 

1) regard semaphores as a lock sign to achieve mutex access to a shared variable.

 

Implementation process:

 

P (mutex); // The initial value of mutex is 1 to access the shared data;

 

V (mutex );

 

Non-critical section

 

2) regard semaphores as the remaining number of shared resources of a certain type to achieve access to a type of shared resources.

 

Implementation process:

 

P (resource); // The initial value of resource is the number of resources N;

 

V (resource); non-critical zone

 

3) use semaphores as the tool for synchronizing data between processes.

 

Implementation process:

 

Critical section C1;

 

P (S );

 

V (S );

 

Critical section C2;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.