System Programming Midterm

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Explain how a conditional expression may be rewritten using an if-then-else statement.

Given a conditional statement cc(A,B,C) (A)?(B):(C) if(A){ B; } else { C; }

Rewrite the following for loop as a while loop.ifor (Index = 1;Index < MAX;Index++) { printf("%d\n",Index); Total += Index; }

Index = 1; while (Index < MAX) { printf("%d\n",Index); Total += Index; Index++; }

-g

Invoke debugging option. This instructs the compiler to produce additional symbol table information that is used by a variety of debugging utilities.

-l

Link with object libraries. This option must follow the source file arguments. The object libraries are archived and can be standard, third party or user created libraries. Probably the most commonly used library is the math library. You must link in this library explicitly if you wish to use the maths functions (don't forget to #include <math.h>). For example:prompt> gcc calc.c -o calc -lm

-O

Optimize the compiled code to level, which is in the range 0 to ... (depends on compiler).

To what standard should programs be written to ensure portability between UNIX variants?

POSIX are a set of standards that can enhance portability.

Explain the underlying mechanism that causes a command such as " ls -la | grep System " to send the output from ls to the input of grep.

Pipelining allows for the chaining for commands. In this example, it This command uses a pipeline (|) which connects the stdout of a command to the stdin of the next command ls = lists files -l = long format(provides additional info) -a = all files and directories grep System = searches for keyword system and prints result

-o

Send the compiled output to file rather than the default a.out

What are the two main lines of UNIX development?

System V and BSD

Explain what the UID and EUID of a process are used for

The UIDs are used for accounting and user-user communication, while the EUIDs are used to control access to files and control signal sending.

How is the first process on a UNIX system created, and what is it (eventually) called?

The first process created is "hand tooled" by the boot process: 1.Power on and run BIOS 2. Read the boot loader, e.g., GRUB, from the boot block (block 0) of the root file system, to select a kernel. 3.GRUB loads a kernel 4. Kernel mounts the root file system, and creates process 0. 5. Process 0 creates (systemd) or init which has a pid of 1. 6. Process 0 then becomes the sched (Sys V) or swapper (BSD) which brings processes into memory 7. init runs getty, which monitors terminal lines and allows users to log in 8. The command shell replaces the getty program when user logs in

What are environment variables?

They are dynamic, named values that can affect the way processes and programs operate within the system. Environment variables provide a means to store and access configuration information, system settings, and other data that programs need to function properly

Define a process.

UNIX is a multi-tasking system, i.e. it can run multiple programs at once. A running program (with its data) is called a process.

What effect does the const qualifier have when applied to a function argument?

When the const qualifier is applied to a function argument in C, it means that the function will not modify the value of the argument.

in globbing what does ^ mean

^ negates some glob expressions

Which of the following lines are recognised by the regexp ^a*b+c{2}? bbbbcc abbccc aaabbbccc aaabbbcccddd bccbccbccbccbcc

^: Anchors the pattern to the start of the string. a*: Matches zero or more consecutive 'a' characters. b+: Matches one or more consecutive 'b' characters. c{2}: Matches exactly two consecutive 'c' characters.

predefined idenifiers

__LINE__ = current source line number __FILE__ = current file name __DATE__ = date of compilation __TIME__ = time of compilation __STDC__ = 1 (only in standard conforming implementations)

Explain the effect of putting the following lines in your .tcshrc file: alias vi vim alias pico vi

alias vi vim: if vi is typed vi it will act as if vim was typed and will then create an alias for vi which will be pico. Essentially routing pico to the vim command.

What does the command man man provide?

displays the manual of the manual

What does the following command output? find / -empty | wc -w

find / -empty: This part of the command searches for empty files and directories starting from the root directory ("/"). The -empty option is used to find empty files and directories. |: This is the pipe symbol, which is used to pass the output of the find command as input to the next command. wc -l: This command is used to count the number of lines in the input it receives from the previous command (in this case, the list of empty files and directories found by find). So, when you run this command, it will count the number of empty files and directories in the root directory and its subdirectories and display the count as the output.

Rewrite the following while loop as a for loop. Index = 1; while (Index < MAX) { printf("%d\n",Index); Total += Index; Index++; }

for (int Index = 1;Index < MAX;Index++) { printf("%d\n",Index); Total += Index; }

What gcc flag is used to link a particular library into a C program? Give an example.

gcc -o my_program my_program.c -lm -l at the end of the line links the math library to c program

What command shows the groups of the owner of the process?

groups

What is the name of the important process whos PID is 1?

init

How are integer constants expressed using decimal, octal, and hexidecimal?

int decimal notation - sequence of digits int octal notation - leading zero int hexidecimal notation - leading 0x or 0X. Characters in either case.

Give an example showing how an array may be initialized when defined.

int numbers[] = {1, 2, 3, 4, 5};

What does the who command do?

lists users currently logged in

in globbing what does ~

means the home directory

What do the getty processes do?

monitors terminal lines and allows users to log in

What commands display environment variables?

printenv

What does the pwd command do?

prints the name of the current working directory

Describe how soft (symbolic) links give a file multiple names in UNIX.

provide a reference to another node in the hierarchy, for the system to follow.

what does the id command tell you?

provides information about the user and the group identity. Such as, UID, GID, supplementary group memberships

Give the command to set a shell variable named NewPath to the current value of the path variable.

set NewPath=$PATH

What command sets an environment variable?

setenv

foreground

the foreground process can be suspended and moved to the background ^Z suspends the foreground process jobs also lists suspended processes bg %N activates a suspended process in the background fg %N also activates a suspended process in the foreground kill %N also terminates a suspended process

______ represents read access for group

040 Way to remember: 2nd slot is for group 4 is for read access

Explain how to make a shell script into a standalone executable.

1. start with #!/bin/tcsh to use interpreter 2. use chmod+x your_script to make it executable 3. ./your_script

______ represents execute access for user

100 Way to remember: 1st slot is for user 1 is for execute access

At a command prompt, what would the following commands do: !17 !gli

!17- executes the command from history that has the number 17 !gli- executes the most recent command that starts with "gli"

Explain the difference between #include <blue.h> and #include "blue.h"?

"" searches directory of source first, then standard places. <> search standard places only.

What would the command kill -9 90210 do?

"kill" used to send signal to process "-9" immediately terminates targeted process without cleanup "90210"-random PID

Write a conditional compilation statement that, depending on whether or not the symbol LOCAL is defined, includes the file string.h from the current directory (LOCAL is defined) or from the standard splace (LOCAL not defined).

#ifdef LOCAL #include "string.h" // Include from the current directory if LOCAL is defined #else #include <string.h> // Include from the standard library if LOCAL is not defined #endif

Explain the difference between these two commands: ls & ps & date & ls ; ps ; date

& is a command seperator which makes them run cocurrently in the background. output unorganized and based on rate of completion ; is a command seperator but makes them run sequentially in the foreground

Explain the difference between placing a string in single and double quotes in a command.

' ' = treated as plain text. it can also be used to run a command within a command " " = subjects string to possibility of variable expansion and other command substitutions

Assuming that the bits in an integer are numbered from 0, from right (least significant) to left (most significant), write an expression using bitwise logical operators to extract the 4 bits numbered 6 to 3 from an integer i1, and shift them all down so that bit numbered 3 ends up in position number 1.

((i1 >> 3) & 0b1111)

How is the information about each UNIX user stored?

(NIS SYSTEM): centrally on the NIS server (NON NIS): on file

in globbing what does * mean

* means any string (including empty)

_____ represents read access for user

400 Way to remember: 1st slot is for user 4 is for read access

chmod 510 MyProgram does _____

400 - read for user 100 - execute for user 010 - execute for group ... and adds permissions to MyProgram

Give the syntax for a function {declaration|definition}.

<function type> <name>(<argument declarations>); <function type> <name>(<argument declarations>){ <local variable definitions> <statements> [return <return value>] }

in globbing what does ? mean

? means any character

What is the difference between a declaration and a definition of a variable or function?

A declaration gives information about something (data type, storage class, variable/function). A definition reserves space and generates code.

How are arrays treated differently from other variables when passed as arguments to functions?

Array names represent the address of the first element thus an array is passed by reference

At what point during a program's execution are {automatic|external|static} variables initialized?

Automatic on invocation External on program start, before main Static on first invocation

What are the key features of {automatic|register|external|static} variables?

Automatic: come into existence on invocation and disappear when the function is exited Register: informs the compiler that it will be used heavily and is stored in machine registers External: defined at the outer level in a file (ex: functions), exist independently of what they are defined outside of, cannot be accessed from any other file Static: makes objects permanent or private, remain in existence after function has exited

Background Processes

Background processes are started by appending a & to the command.While a background process is running the shell continues.Output from the background process is mixed in ... the solution is to send the stdout to a file, leaving stderr to screen.If a background process needs input, it gets suspended (read on). Each process you start from the shell is known as a "job", i.e., you have maximally one foreground job and can have multiple background jobs. Background processes can be controlledjobs lists background processes, and their numberskill %N terminates a background process by job numberfg %N brings a background process into the foreground

List in order the places that a shell checks for the code to execute a user's command.

For each command entered, the shell: 1. Alias Substitution: The shell first checks if the command matches any defined aliases and substitutes it if found. 2. Variable Substitution: It then performs variable substitution on all words not enclosed in single quotes (done in double quotes). 3. Globbing: After that, it performs globbing on all words not enclosed in single quotes. 4. Builtin Execution: Next, the shell checks if the command is a built-in command and executes it if it is. 5. Child Process Creation: If the command is not a built-in, the shell creates a child process. a. Absolute File Name Check: It then checks if the command is an absolute file name and executes it if it is. b. PATH Variable Search: If the command is not an absolute file name, the shell searches each directory listed in the PATH variable to see if the command exists there and executes it if found.

____ represents read access for others

004 3rd slot is for others 4 is for read

___ represents execute access for group

010 Way to remember: 2nd slot is for group 1 is for execute access

______ represents write access for group

020 Way to remember: 2nd slot is for group 2 is for write access

File names cannot contain a(n) ____.

- spaces(never) - single quotes(never) - numeric only names(confusion) - Special Character(confusion)

compiler options

-E Run just the preprocessor -S Stop after producing assembler code in a .s file. -c Suppress the linking process and produce a .o file for each source file listed. Several can be subsequently linked by the gcc command, for example:prompt> gcc file1.o file2.o ...... -o executable

What two files in your home directory are read by your shell when you login?

/etc/csh.cshrc and /etc/csh.login

What file contains the information about users in a non-NIS based UNIX system?

/etc/passwd

What UID does the super-user have?

0

____ represents execute access for others

001 Way to remember: 3rd slot is for others 1 is for execute access

___ represents write access for others

002 3rd slot is for others 2 is for write

What seven items of information are stored about each user of a UNIX system?

1. Name 2. Password 3. User ID 4. Group ID 5. Personal Information 6. Home directory 7. Shell program

_____ represents write access for user

200 Way to remember: 1st slot is for user 2 is for write access

What is the output from the following code segment?int i1 = 27; int i2 = 29; if (i1++ >= --i2) { i1++; printf("%d %d\n",i1++,i2--); } else { i2++; printf("%d %d\n",--i1,++i2); } printf("%d %d\n",i1,i2);

27 30 27 30

What is the output from the following code segment? Index = 1; while (Index < 30) { if (Index % 2 == 0) { Index = Index < 10 ? ++Index : Index +3; continue; } Index = (Index << 1) + 1; if (Index % 6 == 0) { break; } printf("%d\n",Index); }

3 7 15 31

-L

Add directory to the list of directories containing libraries. The linker always looks for standard and other system libraries in /lib and /usr/lib. If you want to link in libraries that you have created or installed yourself (unless you have certain privileges and get the libraries installed in /usr/lib) you have to specify where you files are stored, For example:prompt> gcc prog.c -L /home/myname/mylibs -lmystuff

-I

Add pathname to the list of directories in which to search for #include files with relative filenames. The preprocessor first searches for #include files in directories named with -I options (if any), and then in /usr/include. So to include header files stored in /home/myname/myheaders you would do:prompt> gcc prog.c -I /home/myname/myheaders

Describe how hard links give a file multiple names in UNIX.

Both the original file and the hard link(s) have different names but refer to the same data on the disk ln source_file link_name. 1. all changes made to one file name will be reflected to all files with the same underlying data 2. Can only be made in same file system 3. No distinction between linked and original file

Write code that defines a macro CHOOSE. If the symbol LARGE is defined CHOOSE should return the larger of its two arguments, otherwise it should return the smaller

Choose Macro.c on c preprossessor

Explain the difference between continue and break statements.

Continue: skips the rest of the current iteration of the loop and moves to the next iteration Break: exits a loop entirely

-D

Define symbols either as identifiers (-D identifer) or as values (-D symbol=value) in a similar fashion as the #define preprocessor command.

What is the advantage of having most of UNIX written in C?

Portability: Code written in C can be compiled on different hardware platforms without major modifications. Efficiency: C is a systems programming language that provides low-level control over hardware resources and memory. This allowed UNIX developers to write efficient and compact code, which is crucial for an operating system where performance is critical. Maintainability: C code tends to be more readable and maintainable than lower-level languages like assembly. This made it easier for developers to work on and enhance the UNIX codebase over time. Standardization: By writing UNIX in C, the operating system could be developed with a certain degree of standardization, making it more consistent and predictable across different systems. This was essential for the adoption of UNIX as a standard platform. Reusability: C's modular and structured programming features made it possible to reuse code components within the operating system, which improved development efficiency and contributed to a more coherent system design. Compatibility: C provides a good interface for interacting with hardware and other software components. This compatibility and flexibility made it easier for UNIX to support a wide range of hardware devices and software applications. Openness: By using a high-level programming language like C, UNIX became more open and accessible to developers. It encouraged a collaborative development model and led to the creation of a thriving ecosystem of UNIX utilities and applications. Cross-Platform Development: C is not tied to a specific hardware platform, which made it easier to develop cross-platform applications and system tools for UNIX. This cross-platform capability was important for UNIX's success in diverse computing environments. Transition to New Hardware: As hardware technologies evolved, it was relat

What are the four processes in converting a C program to an executable program? What are the formats of each stage's input and output files?

Preprocessing: Input: The input to the preprocessing stage is typically a C source code file (e.g., .c file). Output: The output of the preprocessing stage is an expanded source code file with preprocessor directives (e.g., #include, #define) processed. The output file typically has a .i extension. Compilation: Input: The input to the compilation stage is the preprocessed source code (.i file) produced in the previous stage. Output: The output of the compilation stage is assembly code (usually in a platform-specific assembly language). The output file often has a .s extension. Assembly: Input: The input to the assembly stage is the assembly code (.s file) produced in the previous stage. Output: The output of the assembly stage is machine code in binary format. This machine code represents the low-level instructions for the specific target architecture. The output file typically has a .o (object) extension. Linking: Input: The input to the linking stage consists of one or more object files (.o files) that may be produced from different source files. Additionally, it includes library files and system libraries (if necessary). Output: The output of the linking stage is the final executable program. It combines the object files, resolves dependencies between them, and includes necessary library code. The output file typically does not follow a specific extension convention (e.g., it may be named without an extension, or it may have an extension like .exe on Windows or no extension on Unix-like systems).

in globbing [<letters>] means

any one of the <letters> (except ...), which can also be a range

What does the following macro do? #define cc(A,B,C) (A)?(B):(C)

cc - conditional A is condition b is the value returned if a is true c is the value returned if a is false

What does the cd command do?

changes directory

What are the four basic data types of C?

char, int, float, double

What is the significance of a leading . on a file name?

designates the file or directory as hidden. Which means they arent normally displayed in directory lisitings and can be used to avoid accidental deletion.

Explain the mechanism by which multiple physical disk partitions are viewed as a single file system in UNIX.

disk partitioning and volume management

What commands removes an environment variables?

unsetenv

How is it posssible for a program to read a network connection as a file?

using a socket link

What command lists the users in an NIS based UNIX system?

ypcat passwd


Set pelajaran terkait

Ch. 52: Assessment and Management of Patients with Endocrine Disorders PREPU

View Set

Chapter 37: Cardiac Glycosides, Antianginals, and Antidysrhythmics, Chapter 40: Anticoagulants, Antiplatelets, and Thrombolytics, Chapter 41: Antihyperlipidemics and Peripheral Vasodilators, Chapter 54: Drugs for Hemophilia, Chapter 53: Management of...

View Set

CA1: Introduction to Liabilities (True or False)

View Set

American Political Parties (POLS 3035) - Exam 3

View Set

The Formation of Soil (Quiz Questions)

View Set