Operating Systems 2022F: Assignment 1

From Soma-notes
Revision as of 15:09, 17 September 2022 by Soma (talk | contribs) (→‎Questions)
Jump to navigation Jump to search

This Assignment is still being developed.


  1. [2] There are three functions defined in 3000menu.c. Which lines in 3000menu.s implement each of those functions? How do you know those are the correct lines?
  2. [1] Why is environ referenced but not defined in 3000menu.s?
  3. [2] What lines in 3000menu.s define the menu array of character arrays (the declaration on line 10 of 3000menu.c)? What lines define the constant string arrays it points to?
  4. [2] Which lines of 3000menu.s correspond to lines 72-76 of 3000menu.c, inside of main()? What do each of these lines do?
  5. [2] The call to getline on line 56 of 3000menu.c does an implicit malloc() call, requiring a call to free() on line 58. Do these calls result in additional system calls? How do you know?
  6. [2] If we replace line 27 of 3000menu.c with "pid = 0;" how does the behavior of the program change? Why?
  7. [2] In line 30 of 3000menu.c, we replace one of the string pointers in menu with NULL.
    1. [1] What is the purpose of this line?
    2. [1] This doesn't change the menu that is displayed to the user. Why not?
  8. [2] How does the 3000menu.c's behavior change if you delete line 34, the call to wait? Why?
  9. [5] Replace line 29 in 3000menu.s (the assembly code version) with the following two lines:
        mov $57,%eax
    Create a new binary with the command gcc -O -z lazy 3000menu.s -o 3000menu-q2. It should run the same as before.
    1. [1] Is 3000menu-q2 statically or dynamically linked?
    2. [1] How does the library call behavior of 3000menu-q2 differ from 3000menu? Why?
    3. [1] How does the system call behavior of 3000menu-q2 differ from 3000menu? Why?
    4. [2] Could you modify 3000menu.c so that it compiled to essentially identical assembly as 3000menu-q2? Why or why not? (You only need to consider standard C functionality.)



/* 3000menu.c */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

extern char **environ;

char *menu[] = {

int QUIT = 3;

void run_program(int choice)
        pid_t pid;
        int status;
        printf("Running %s\n", menu[choice]);

        pid = fork();

        if (pid == 0) {
                menu[choice+1] = NULL;
                execve(menu[choice], menu + choice, environ);
        } else {
                pid = wait(&status);

int choose_program(void)
        int i = 0;
        int choice;
        char *input = NULL;
        size_t result, n = 0;

        printf("\nChoose a program to run:\n\n");
        while (menu[i] != NULL) {
                printf("%d. %s\n", i + 1, menu[i]);

        printf("\nYour choice? ");

        result = getline(&input, &n, stdin);
        choice = atoi(input);

        if ((choice > 0) && (choice <= i)) {
                return choice - 1;
        } else {
                return -1;

int main()
        int program;

        while (1) {
                program = choose_program();

                if (program == QUIT) {
                        return 0;
                if (program != -1) {


	.file	"3000menu.c"
	.section	.rodata.str1.1,"aMS",@progbits,1
	.string	"Running %s\n"
	.globl	run_program
	.type	run_program, @function
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movl	%edi, %ebx
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movslq	%edi, %rdx
	leaq	menu(%rip), %rax
	movq	(%rax,%rdx,8), %rdx
	leaq	.LC0(%rip), %rsi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk@PLT
	call	fork@PLT
	testl	%eax, %eax
	je	.L5
	leaq	4(%rsp), %rdi
	call	wait@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L6
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	leaq	menu(%rip), %rax
	leal	1(%rbx), %edx
	movslq	%edx, %rdx
	movq	$0, (%rax,%rdx,8)
	movslq	%ebx, %rbx
	leaq	(%rax,%rbx,8), %rsi
	movq	(%rax,%rbx,8), %rdi
	movq	environ(%rip), %rdx
	call	execve@PLT
	movl	$-1, %edi
	call	exit@PLT
	call	__stack_chk_fail@PLT
	.size	run_program, .-run_program
	.section	.rodata.str1.1
	.string	"\nChoose a program to run:\n"
	.string	"%d. %s\n"
	.string	"\nYour choice? "
	.globl	choose_program
	.type	choose_program, @function
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movq	$0, 8(%rsp)
	movq	$0, 16(%rsp)
	leaq	.LC1(%rip), %rdi
	call	puts@PLT
	movq	menu(%rip), %rcx
	testq	%rcx, %rcx
	je	.L12
	movl	$1, %ebx
	leaq	.LC2(%rip), %r13
	leaq	-8+menu(%rip), %r12
	movl	%ebx, %ebp
	movl	%ebx, %edx
	movq	%r13, %rsi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk@PLT
	addq	$1, %rbx
	movq	(%r12,%rbx,8), %rcx
	testq	%rcx, %rcx
	jne	.L9
	leaq	.LC3(%rip), %rsi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk@PLT
	leaq	16(%rsp), %rsi
	leaq	8(%rsp), %rdi
	movq	stdin(%rip), %rdx
	call	getline@PLT
	movl	$10, %edx
	movl	$0, %esi
	movq	8(%rsp), %rdi
	call	strtol@PLT
	movq	%rax, %rbx
	movq	8(%rsp), %rdi
	call	free@PLT
	testl	%ebx, %ebx
	jle	.L13
	cmpl	%ebx, %ebp
	jl	.L13
	leal	-1(%rbx), %eax
	movq	24(%rsp), %rdx
	subq	%fs:40, %rdx
	jne	.L16
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	movl	$0, %ebp
	jmp	.L8
	movl	$-1, %eax
	jmp	.L7
	call	__stack_chk_fail@PLT
	.size	choose_program, .-choose_program
	.globl	main
	.type	main, @function
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	call	choose_program
	cmpl	%eax, QUIT(%rip)
	je	.L22
	cmpl	$-1, %eax
	je	.L19
	movl	%eax, %edi
	call	run_program
	jmp	.L19
	movl	$0, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
	.size	main, .-main
	.globl	QUIT
	.align 4
	.type	QUIT, @object
	.size	QUIT, 4
	.long	3
	.globl	menu
	.section	.rodata.str1.1
	.string	"/usr/bin/ls"
	.string	"/usr/bin/whoami"
	.string	"/usr/bin/top"
	.string	"QUIT"
	.section	.data.rel.local,"aw"
	.align 32
	.type	menu, @object
	.size	menu, 40
	.quad	.LC4
	.quad	.LC5
	.quad	.LC6
	.quad	.LC7
	.quad	0
	.ident	"GCC: (Ubuntu 11.2.0-19ubuntu1) 11.2.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
	.string	"GNU"
	.align 8
	.long	0xc0000002
	.long	3f - 2f
	.long	0x3
	.align 8