csc 322 final

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

What is in a UNIX .a file?

Library files are given a .a extension.

Which signal cannot be ignored or caught by a receiving process?

SIGKILL and SIGSTOP

Given the multidimensional array float values[4][3]; what is the equivalent pointer expression for values[2][2]?

^(^(values+2)+2)

Difference between typedef and #define:

#define in C is a directive which is used to #define alias. The typedef is used to give data type a new name. typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc. typedef interpretation is performed by the compiler where #define statements are performed by preprocessor. #define should not be terminated with a semicolon, but typedef should be terminated with semicolon. #define will just copy-paste the definition values at the point of use, while typedef is the actual definition of a new type. typedef follows the scope rule which means if a new type is defined in a scope (inside a function), then the new type name will only be visible till the scope is there. In case of #define, when preprocessor encounters #define, it replaces all the occurrences, after that (No scope rule is followed).

Point out the errors in this program, which prevent it from compiling, and even if it did compile, from running properly. #include <stdio.h> #include <string.h> int main(int argv,char *argv[]) { char Buffer[128]; char *Data; Buffer = "the cat sat on the mat"; Data = &Buffer[5]; Buffer = Data; strcpy(Data,Buffer); return(0); }

- Has to be argc for argument count - strcpy(buffer, "the cat sat on the mat"); - Cant have an char array set equal to a pointer(Buffer = Data;)

What advantage do the execlp and execvp calls have over execl and execv?

- execvp- combines the advantages of execlp and execv - execlp- searches the directories in the PATH environment variable for the program. - execl - The program_path is the name of the file to execute. - execv - provides the arguments as an array of pointers, in the same way as they are received by main. The last pointer must be NULL.

Give an example showing how an array of pointers can be used to create a two dimensional array in which the rows are of differing lengths.

1) int A[10][10], *B[10]; defines A to be a 2D array of 100 ints and B to be an array of 10 pointers to int. 2) A[4][6] and B[4][6] are both syntactically legal. A[4][6] is the 7th element in the 5th array. B[4][6] is the memory location 6 ints beyond the address given in the 5th array element, equivalent to *(B[4] + 6).

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.

1234 -> 0012 positions are (4)(3)(2)(1) i1>>2;

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 exact output from the following code? Indicate spaces with an upsidedown triangle. int i1 = 27; float f1 = 13.13; float f2 = -45.45; char data[] = "Bonzai!"; printf("%3d %7.3f %-10s %f %o\n",i1,f1,data,f2,i1);

27^ 13.130 Bonzai!^^^ -45.450001 33 Three digits, at most 7 up front and then three digits, 10 characters, a float(6 decimals), and an octal(27/8 = 3 remainder 3)

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

28 0x1C /* = Hexadecimal representation for decimal 28 */ 034 /* = Octal representation for decimal 28 */ Decimal: 32179 (base 10 just numbers) Octal: 076663 (base 8) Hexadecimal: 0x7DB(base 16)

Index = 1; while (Index < 30) { if (Index % 2 == 0) { Index = Index < 10 ? ++Index : Index +3; continue; } Index = (Index << 1) + 1; /// shifts to the left once, add zero if (Index % 6 == 0) { break; } printf("%d\n",Index); }

3 7 15 31

What are the values of the variables after the following code is executed with input 56789 1234 45a72? int i1; float f1; char name[50]; scanf("%2d %f %*d %2s",&i1,&f1,name);

56 789.000000 45 *d skips next set of numbers

Explain the difference between a union and struct

A struct is a block of memory that stores several data objects, where those objects don't overlap. A union is a block of memory that stores several data objects, but has only storage for the largest of these, and thus can only store one of the data objects at any one time.

In terms of make, what are targets, dependencies, and actions?

A target is usually the name of a file that is generated by a program; examples of targets are executable or object files. A target can also be the name of an action to carry out, such as `clean' A dependency is a file that is used as input to create the target. A target often depends on several files. A command is an action that make carries out. A rule may have more than one command, each on its own line. Please note: you need to put a tab character at the beginning of every command line! This is an obscurity that catches the unwary. targets: prerequisites -----command --------command --------command

What user information stored in the /etc/passwd file?

A user ID is associated with each user login name in the password file /etc/passwd

Which of the following are keywords in C?

Auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short , signed, sizeof, static, struct switch, typedef, union, unsigned, void, volatile, while

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

Auto: It is a default storage class, used to allocate and deallocate when programs flow enters and leaves variables scope Extern: It is a global variable, declare variable without defining it Static: It is a local variable which is capable of returning a value even when control is transferred to the function call, used to preserve varible value when out of scope Register: It is a variable which is stored inside a Register to make them faster

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

Automatic: When the variable or object is invoked (varible defined) and disappears when the function has exited.; at declaration External: at the time compilation Static variables are executed at compile-time and they remain permanent even after the function has exited. #define

What is an enumerated type?

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

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

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

char *String1; char String2[128];

Similarity: Both array and pointer are passed by reference. Difference: *String1 is a dereferenced pointer while String2[128] is memory location which has a pointer pointing to its first value/location. The array makes space for 128 characters while pointer makes space for one character pointer

What is the difference between *i1 in the following two lines? int *i1; *i1 = 27;

The difference is that int i1 simply declares an integer pointer i1 and i1 =27 dereferences and defines that integer pointer with a value of 27.

What information is returned by the getpid, getppid, and getpgrp system calls?

The getpid() - process ID of the calling process. The getpgrp() function returns the process group ID of the calling process. getppid() function returns the parent process ID of the calling process.

typedef a structure suitable for holding information about a radio station, including it's 4 character code name (e.g., ZETA), it's catch phrase (e.g., "ZETA Rocks"), it's frequency (e.g., 94.9), and the number of employees (e.g., 27).

Typedef struct radioStation{ Char code_name[4]; char phrase[50]; Float frequency; Int number_employees; } radioStationInfo;

What UNIX program is used to create libraries?

ar -rv libmystuff.a *.o

Explain the difference between continue and break statements.

break statement: This statement terminates the smallest enclosing loop continue statement: This statement skips the rest of the loop statement and starts the next iteration of the loop to take place.

Why should fgets be used in preference to gets?

fgets has an input limit while gets doesn't. fgets can also decide where to take the input from.

What system call is designed for reading a password from a user?

getpwent();

What is the difference between the return values of the getuid and geteuid system calls?

getuid() will return ID of user who runs program, while geteuid() will return effective ID of user who own program executable. The real IDs are used for accounting and user-user communication, while the effective IDs are used to control access to files and control signal sending.

After a call to uname, what information is stored in the struct utsname pointed to be the parameter?

information about current operating system

What are the four basic data types of C?

int , char , float, double

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

int array[10] = {1, 2, 3};

The following problem could suffer from a divide by zero error. Rewrite it adding an assert statement to prevent the potential error. int i1,i2; scanf("%d %d",&i1,&i2); printf("%d\n:,i1/i2);

int i1,i2; scanf("%d %d",&i1,&i2); assert i2 = 0,"error" printf("%d\n:,i1/i2);

Write a program that creates a second process, and then in both processes outputs the process ID and the owners user ID.

int main() int CPID; print("parent is %d\n", getpid()); if ((CPID = fork()) == -1){ exit(EXIT_FAILURE); } if(CPID==0){ print("child is %d and parent is %d", getpid(), getppid()); exit(EXIT_SUCCESS); }

Write a program that declares an array of 10 pointers to char, then reads 10 lines from the user, dynamically allocates enough memory for each line after it is read, and makes successive members of the array point to the dynamically allocated memory. When done, the program must then release all the dynamically allocated memory.

int main(){ char *Array[10]; char line[100]; int length; int i = 0; for(int i = 0; i < 10; i++){ fgets(line, 100, stdin); length = strlen(line); line[length] = '\0'; Array[i] = (char)malloc(sizeof(char) * length); strcpy(Array[i], line); } for(int i = 0; i < 10; i++){ printf("Pointer %d: Line: %strcpy", i+1, Array[i]); } for(int i = 0; i < 10; i++) free(Array[i]); return 0; }

Write a program that takes integers as command line arguments, sums them, and returns the sum back to the operating system.

int main(int argc, char *argv[]) { int sum = 0; for(int i=1;i<argc;i++){ int num = atoi(argv[i]); sum = sum + num; } return sum; }

Name three system calls that cause (directly or indirectly) a signal to be sent to a process.

kill(), abort(), exit()

Below is the content of a makefile. Assume that the files part1.c and part3.c have just been modified. Write down the sequence of command activations in the correct order when make is run. all: part1.o part2.o gcc part1.o part2.o -o whole part1.o: part1.c part3.o gcc -c part1.c gcc part1.o part3.o -o part3.out part2.o: part2.c gcc -c part2.c part3.o: part3.c gcc -c part3.c

make part3.o part1.o all

Explain the difference between memcpy and memmove.

memcpy - Copy one buffer into another (bytewise version of strncpy). memmove - Moves a number of bytes from one buffer to another. Same as memcpy except the buffers may overlap.

Given the structure definitions typedef struct { int Counter; float Average; } hit_rate_type; typdef struct { char *Name; hit_rate_type Killer; } murder_type; murder_type *JackTheRipper; write code that mallocs memory for JackTheRipper, stores the name "Jack the Ripper" in the Name field, and stores 99 in the Counter.

murder_type *JackTheRipper; JackTheRipper = (murder_type*)malloc(sizeof(murder_type)); JackTheRipper -> Killer.Counter = 99; JackTheRipper ->Name = (char*)malloc(sizeof("Jack The Ripper")); strcpy(JackTheRipper->Name, "Jack The Ripper"); printf("%s\n",JackTheRipper->Name); printf("%d\n", JackTheRipper->Killer.Count); free(JackTheRipper->Name); free(JackTheRipper);

What does the following program output? #include <stdio.h> #include <sys/types.h> #include <pwd.h> int main(int argc,char *argv[]) { struct passwd *P; while ((P = getpwent()) != NULL) { if (P->pw_uid == geteuid()) { printf("%s\n",P->pw_name); } } return(0); }

pw_name

Explain what realloc does, including the cases when the new size does not equal the old size.

realloc changes the size of a block of memory previously allocated with malloc or calloc whilst preserving the contents. Realloc returns a pointer to the new block or NULL if unsuccessful, in which case old is unchanged. if the new size does not equal the old size, then old goes unchanged changes size of mem; new block may be in different;

Explain how setjmp and longjmp can be used to retreat from complicated situations in one step.

setjump(jmp_buf buf) : uses buf to remember current position and returns 0. longjump(jmp_buf buf, i) : Go back to place buf is pointing to and return i . The main feature of these function is to provide a way that deviates from standard call and return sequence. This is mainly used to implement exception handling in C. setjmp can be used like try (in languages like C++ and Java). The call to longjmp can be used like throw (Note that longjmp() transfers control to the point set by setjmp()).

What are the three predefined IO streams in UNIX, and what devices are they associated with by default?

stdin - initially associated with the keyboard stdout - initially associated with the screen stderr - initially associated with the screen their type is FILE *

What errors might the following code produce? char string[128]; char *name; strcpy(string,name); strcpy(name,"old stuff");

strcpy(string,name); will cause a segmentation fault as name is not pointing to anything. It is not defined or initialized and as such it exists as a pointer pointing to essentially to junk (non-Null). This "junk" may or may not be owned by the user and as such causes a segmentation error at strcopy(string, name). (donest have access to memory) strcopy(name, "old stuff") would cause a bus error as the name doesn't formally exist and as such if name doesn't exist how can "old stuff" be copied into nothing (non-Null). As such, it would cause a bus error. (what its pointing to doesn't have memory address)

What does the & operator return when applied to a variable?

the address of an object.

Which system call can be used to limit the amount of CPU time a process can use? Which system call can be used to find out how much CPU time the process did use?

ulimit time process_name

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

z = a > b ? a : b; Now here z will have a if a>b else it will get b So it can be rewritten as: if(a>b) { z=a; } else { z=b; }

What system call can be used to get the IP address of a machine?

~/hostname -i


Set pelajaran terkait

Civil War and Reconstruction Unit Test

View Set

Microeconomics Chapter 14- Exam 2

View Set

EGCC - BUS101 Intro to Business - Ch. 3-4 - Quiz

View Set

Dickens Vocabulary Main Set (A Christmas Carol)

View Set

Unit 3 112 yamada and meng ch 6 Principles of Visual Analysis of EEG

View Set

Massage Theory Intensive THEORY QUIZZES

View Set

4.3.1 Define the Terms Force, Speed, Velocity, Displacement, Acceleration, Momentum, and Impulse

View Set