16281035- Operating System Experiment 1

16281035- Operating System Experiment 1

My github link, in which all of the source files

**16281035 **

(Note: The All experiments are done in Linux)

A, (experimental system call) for system calls different packages.

Requirements: 1, with reference to the following program URL. Read the API interface functions with getpid () directly calls and interrupts compilation with a system call two ways to call the Linux operating system calls getpid programs are run (Will getpid system call number? Interrupt vector number is the number of linux system calls ?). 2, the exercise machine to complete 1.13.

http://hgdcg14.blog.163.com/blog/static/23325005920152257504165/

A: getpid system call number is 14H, interrupt vector number is 80H.
Here Insert Picture Description

Code a result:
Here Insert Picture Description

Here Insert Picture Description
Code run two results:
Here Insert Picture Description

3, pintos read operation for the system on behalf of the source code, the flowchart shown implemented system calls.
Here Insert Picture Description
II (concurrent experiments) completed according to the following code following experiment.

Claim:

1, compile and run the program (cpu.c), the output was observed, indicating that the program function.

(Compile command: gcc -o cpu cpu.c -Wall) (execute the command: ./ cpu)

A: The program features: loop output parameter passed to the screen, if no parameters, output usage: cpu

The results in the following output:

2, and run again in the following observations: execute the command: ./ cpu A &; ./cpu B &; ./cpu C &; ./cpu D & cpu to run the program a few times? What are the characteristics and laws of order in which they are running? Please characteristics combined with the operating system will be explained.

A: As long as no manual stop, CPU has been run.

** Run features: the order of running is uncertain, I guess the program is executed concurrently, it may be a program has not yet ended, another program has already begun, without waiting for a room that is **** program complete the program before the next program. ** So our four programs running at the same time in the CPU, but the speed is fast or slow, so can there be such output.

Here Insert Picture Description

1 #include <stdio.h>

2 #include <stdlib.h>

3 #include <sys/time.h>

4 #include <assert.h>

5 #include “common.h”

6

7 int

8 main(int argc, char *argv[])

9 {

10 if (argc != 2) {

11 fprintf(stderr, “usage: cpu \n”);

12 exit(1);

13 }

14 char *str = argv[1];

15 while (1) {

16 spin(1);

17 printf("%s\n", str);

18 }

19 eturn 0;

20

三、(内存分配实验)根据以下代码完成实验。

要求:

1、阅读并编译运行该程序(mem.c),观察输出结果,说明程序功能。(命令: gcc -o mem mem.c –Wall)

答:程序的功能是新建进程并打印进程识别码

2、再次按下面的命令运行并观察结果。两个分别运行的程序分配的内存地址是否相同?是否共享同一块物理内存区域?为什么?命令:./mem &; ./mem &

答:内存地址不相同,不共享同一物理内存区域,因为在同一物理内存区的两个进程识别码不可能相同,他们可能是运行在不同内核里面的程序。

Here Insert Picture Description

1 #include <unistd.h>

2 #include <stdio.h>

3 #include <stdlib.h>

4 #include “common.h”

5

6 int

7 main(int argc, char *argv[])

8 {

9 int *p = malloc(sizeof(int)); // a1

10 assert(p != NULL);

11 printf("(%d) address pointed to by p: %p\n",

12 getpid(), p); // a2

13 *p = 0; // a3

14 while (1) {

15 Spin(1);

16 *p = *p + 1;

17 printf("(%d) p: %d\n", getpid(), *p); // a4

18 }

19 return 0;

四、(共享的问题)根据以下代码完成实验。

要求:

1、 阅读并编译运行该程序,观察输出结果,说明程序功能。(编译命令:gcc -o thread thread.c -Wall –pthread)(执行命令1:./thread 1000)

答:程序功能:创建两个线程对counter进行累加操作,并输出初始值和累加后的值**。**

2、 尝试其他输入参数并执行,并总结执行结果的有何规律?你能尝试解释它吗?(例如执行命令2:./thread 100000)(或者其他参数。)

答:规律:可以看到当参数大到一定程度时,输出的值就不再是传进参数的2倍了,可能是因为CPU处理的过程时间太长,两个线程的运行发生了时间上的重合,其中一个线程在进行累加操作时,另一个线程读入了过时的共享变量counter的数据,造成了累加值不足的情况。

3、 提示:哪些变量是各个线程共享的,线程并发执行时访问共享变量会不会导致意想不到的问题。

Here Insert Picture Description

1 #include <stdio.h>

2 #include <stdlib.h>

3 #include “common.h”

4

5 volatile int counter = 0;

6 int loops;

7

8 void *worker(void *arg) {

9 int i;

10 for (i = 0; i < loops; i++) {

11 counter++;

12 }

13 return NULL;

14 }

15

16 int

17 main(int argc, char *argv[])

18 {

19 if (argc != 2) {

20 fprintf(stderr, “usage: threads \n”);

21 exit(1);

22 }

23 loops = atoi(argv[1]);

24 pthread_t p1, p2;

25 printf(“Initial value : %d\n”, counter);

26

27 Pthread_create(&p1, NULL, worker, NULL);

28 Pthread_create(&p2, NULL, worker, NULL);

29 Pthread_join(p1, NULL);

30 Pthread_join(p2, NULL);

31 printf(“Final value : %d\n”, counter);

32 return 0;

33

Guess you like

Origin blog.csdn.net/qq_42452405/article/details/88578952