shaico111 commited on
Commit
eff42de
·
verified ·
1 Parent(s): bbfeafd

Upload batch 343 of 407

Browse files
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 10 - Shared Memory/shm4.c ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <unistd.h>
4
+ #include <sys/wait.h>
5
+ #include <sys/types.h>
6
+ #include <sys/ipc.h>
7
+ #include <sys/shm.h>
8
+ #include <string.h>
9
+ #include <sys/sem.h>
10
+
11
+ union semun
12
+ {
13
+ int val;
14
+ struct semid_ds * buff;
15
+ unsigned short *array;
16
+ struct seminfo *_buf;
17
+ };
18
+
19
+ char *sharestr;
20
+ int shmid, s;
21
+
22
+ int semid;
23
+ union semun semarg,semarg2;
24
+ int i;
25
+ struct sembuf sops[1];// see declaration of sembuf in Google
26
+
27
+ void main()
28
+ {
29
+ semid = semget ( IPC_PRIVATE , 1 , 0600 );// 1- create only one semaphore
30
+ semarg.val = 1;//semaphore is unlocked
31
+ semctl ( semid , 0 , SETVAL , semarg );// 0 - only one semaphore . SETVAL - the command
32
+
33
+ shmid = shmget ( IPC_PRIVATE , 100, 0600 );// the size of the shared memory is 100 bytes
34
+ if ( fork()==0 ) {
35
+ printf ("child : try to lock the semapore \n");
36
+ sops->sem_op = -1; // decrease the sem. from 0 to 1
37
+ sops->sem_num = 0;
38
+ sops->sem_flg = 0;
39
+ semop ( semid , sops , 1 ); // operate the increase
40
+ int val = semctl(semid,0,GETVAL,semarg);
41
+
42
+ printf ("child : semapore is locked now. the semaphore value is %d\n",val);
43
+ sharestr=(char *)shmat ( shmid , NULL , 0600 );
44
+ strcpy ( sharestr , "Writing something to shared memory" );
45
+ shmdt ( sharestr );
46
+
47
+ sops->sem_op = 1; // increase the sem. from 0 to 1 . unlock the sempahore
48
+ sops->sem_num = 0;
49
+ sops->sem_flg = 0;
50
+ semop ( semid , sops , 1 ); // operate the increase*/
51
+ int val2 = semctl(semid,0,GETVAL,semarg);
52
+
53
+ printf ("child : semapore is unlocked now. the semaphore value is %d\n",val2);
54
+
55
+ printf("Child finished !\n");
56
+ }
57
+ else
58
+ {
59
+ int flag = 1;
60
+ int val=0;
61
+ do {
62
+ sleep(1);
63
+ val = semctl ( semid , 0, GETVAL , semarg2 );
64
+ printf("Parent: semaphore value is = %d\n",pval);
65
+ if (pval == 1) {flag=0;printf("Parent : Semaphore is free\n");
66
+ } while (flag == 1);
67
+ printf("Parent is going to lock the semaphore\n");
68
+ sops->sem_op = -1;
69
+ semop ( semid , sops , 1 );
70
+ sharestr=(char *)shmat ( shmid , NULL , 0600 );
71
+ printf ( "%s\n" , sharestr );
72
+ shmdt ( sharestr );
73
+ printf("Parent is going to delete the semaphore\n");
74
+ shmctl ( shmid , IPC_RMID , 0 );
75
+ }
76
+ }
77
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/1.c ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <sys/types.h>
3
+ #include <unistd.h>
4
+
5
+ void main ()
6
+ {
7
+ int pid ;
8
+
9
+ if ( (pid = fork()) == 0 )
10
+ printf("1") ;
11
+ else
12
+ printf("2") ;
13
+ }
14
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/10.c ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <sys/types.h>
4
+ #include <sys/wait.h>
5
+ #include <unistd.h>
6
+
7
+
8
+ void main()
9
+ {
10
+ int pid,pid1,pid2,status;
11
+
12
+ if ((pid1=fork())==0)
13
+ printf("in child 1\n");
14
+ else
15
+ if ((pid2=fork())==0)
16
+ printf("in child 2\n");
17
+ else
18
+ {
19
+ pid=wait(&status);
20
+ pid=wait(&status);
21
+ if (pid==pid1)
22
+ printf("child 1 finished\n");
23
+ if (pid==pid2)
24
+ printf("child 2 finished\n");
25
+ }
26
+ }
27
+
28
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/11.c ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <sys/types.h>
4
+ #include <sys/wait.h>
5
+ #include <unistd.h>
6
+
7
+
8
+ void main()
9
+ {
10
+ int pid_a,pid_b,pid1,pid2,status;
11
+
12
+ if ((pid1=fork())==0)
13
+ printf("in child 1\n");
14
+ else
15
+ if ((pid2=fork())==0)
16
+ printf("in child 2\n");
17
+ else
18
+ {
19
+ pid_a=wait(&status);
20
+ if (pid_a==pid1 )
21
+ printf("child 1 finished\n");
22
+ if (pid_a==pid2 )
23
+ printf("child 2 finished\n");
24
+ pid_b=wait(&status);
25
+ if (pid_b==pid1)
26
+ printf("child 1 finished\n");
27
+ if (pid_b==pid2)
28
+ printf("child 2 finished\n");
29
+ }
30
+ }
31
+
32
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/2.c ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <sys/types.h>
3
+ #include <unistd.h>
4
+ void main ()
5
+ {
6
+ int pid ;
7
+
8
+ if ( (pid = fork()) == 0 )
9
+ printf("1") ;
10
+ else
11
+ printf("2") ;
12
+ printf("3");
13
+ }
14
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/3.c ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <sys/types.h>
3
+ #include <sys/wait.h>
4
+ #include <unistd.h>
5
+ void main ()
6
+ {
7
+ int pid , stat;
8
+
9
+
10
+ //fork returns 0 to son, son's pid to parent
11
+ pid = fork();
12
+ if ( pid == 0 ) //son
13
+ printf("fork returned zero. I am the son. my pid is %d parent's pid is %d\n", getpid(), getppid()) ;
14
+ else //parent
15
+ {
16
+ int result;
17
+ result = wait(&stat);
18
+ printf("I am the father. Fork returned %d, wait returned %d, stat is %d, my pid is %d", pid, result, stat, getpid()) ;
19
+ }
20
+ }
21
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/4.c ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <sys/types.h>
3
+ #include <sys/wait.h>
4
+ #include <unistd.h>
5
+ #include <errno.h>
6
+
7
+ /* executable file fork4 does not exist */
8
+
9
+ void main ()
10
+ {
11
+ int pid , stat;
12
+
13
+ if ( (pid = fork()) == 0 )
14
+ {
15
+ int result;
16
+ result = execl("/u/grad/sasha/fork4","fork4",NULL);
17
+ printf("exec returned %d and errno is %d\n", result, errno) ;
18
+ }
19
+ else
20
+ {
21
+ wait(&stat);
22
+ printf("2") ;
23
+ }
24
+ }
25
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/5.c ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <sys/types.h>
4
+ #include <sys/wait.h>
5
+ #include <unistd.h>
6
+
7
+ /* there is no program called fork5 */
8
+
9
+ void main ()
10
+ {
11
+ int pid , stat;
12
+
13
+ if ( (pid = fork()) == 0 )
14
+ {
15
+ execl("/u/grad/sasha/fork5","fork5",NULL);
16
+ printf("1") ;
17
+ exit(1);
18
+ }
19
+ else
20
+ {
21
+ wait(&stat);
22
+ printf("2") ;
23
+ if (stat != 0 )
24
+ printf("3");
25
+ }
26
+ }
27
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/6.c ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <sys/types.h>
4
+ #include <sys/wait.h>
5
+ #include <unistd.h>
6
+
7
+ /* there is a program called date */
8
+
9
+
10
+
11
+
12
+ int main ()
13
+ {
14
+ int pid , stat;
15
+
16
+ if ( (pid = fork()) == 0 )
17
+ {
18
+ execl("/usr/bin/date","date",NULL);
19
+ printf("- 1(child) -\n") ;
20
+ exit(1);
21
+ }
22
+ else
23
+ {
24
+ wait(&stat);
25
+ printf("- 2(parent) -\n") ;
26
+ printf("- stat = <%d> -\n",stat) ;
27
+ if (stat != 0 )
28
+ printf("- 3(parent,child is failed) -\n");
29
+ else
30
+ printf("- 3(parent,child is succeeded) -\n");
31
+ }
32
+ return 0;
33
+ }
34
+
שנה ב/סמסטר ב/מערכות הפעלה/תרגולים/תרגול 3 ו-4 - תהליכים_Processes - דוגמאות קוד/7.c ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <sys/types.h>
4
+ #include <sys/wait.h>
5
+ #include <unistd.h>
6
+
7
+ /* there is a program called stam (prints 4)*/
8
+ int main()
9
+ {
10
+ int pid , stat;
11
+
12
+ if ( (pid = fork()) == 0 )
13
+ {
14
+ execl("./stam","stam",NULL);
15
+ printf("\n- 1 -\n") ;
16
+ exit(0);
17
+ }
18
+ else
19
+ {
20
+ wait(&stat);
21
+ printf("- 2 -\n") ;
22
+ if (stat != 0 )
23
+ printf("- 3 -\n");
24
+ }
25
+ return 0;
26
+ }
27
+