C Programming Fundamentals Quiz

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What command is used to compile a C program named 'teste.c'?

  • gcc -o teste teste.c (correct)
  • cc -execute teste.c
  • gcc -run teste.c
  • gcc compile teste.c

The option '-Wall' is used to disable warning messages during compilation.

False (B)

What is the purpose of the '-O' option in gcc?

'-O' option is used to optimize the generated code.

The command to execute a compiled program is './______'

<p>teste</p> Signup and view all the answers

What does the command 'gcc -O3 -Wall -c prog.c' perform?

<p>Only compiles 'prog.c' into an object file (A)</p> Signup and view all the answers

It is possible to combine the two compilation steps into one command using gcc.

<p>True (A)</p> Signup and view all the answers

What file is generated after the first step of compiling 'prog.c'?

<p>'prog.o'</p> Signup and view all the answers

What does the function wait() return when called with no child processes?

<p>-1 (A)</p> Signup and view all the answers

If a process calls wait() and all its child processes are still running, it will return immediately.

<p>False (B)</p> Signup and view all the answers

What action must be taken if execlp() fails in the given code segment?

<p>exit(1)</p> Signup and view all the answers

In the code provided, the command list is defined as char *cmd[] = {____};

<p>“who”, “ls”, “date”</p> Signup and view all the answers

Match the following actions with their descriptions:

<p>fork() = Creates a new process execlp() = Executes a program wait() = Suspends the calling process exit() = Terminates the process</p> Signup and view all the answers

What happens in the parent process after calling fork()?

<p>It continues executing independently of the child. (A), It waits for the child process. (B)</p> Signup and view all the answers

Raw files are considered the least efficient and slowest method for storing and retrieving data.

<p>False (B)</p> Signup and view all the answers

What will the program output after the child process completes?

<p>child finished</p> Signup and view all the answers

What does the function ftell return?

<p>The current position of the cursor in the file (B)</p> Signup and view all the answers

The fseek function can only move the cursor forward in the file.

<p>False (B)</p> Signup and view all the answers

What are the three constants that can be used for the origin parameter in fseek?

<p>SEEK_SET, SEEK_CUR, SEEK_END</p> Signup and view all the answers

The function __________ is equivalent to using fseek to move to position 0 in the file.

<p>rewind</p> Signup and view all the answers

Which of the following statements about the fseek function is correct?

<p>It requires a file pointer and a displacement value. (A)</p> Signup and view all the answers

Match the following fseek parameters with their descriptions:

<p>SEEK_SET = Indicates the beginning of the file SEEK_CUR = Indicates the current position of the cursor SEEK_END = Indicates the end of the file</p> Signup and view all the answers

The displacement value in fseek can only be a positive number.

<p>False (B)</p> Signup and view all the answers

What would the following line of code do? fseek(fichier, -4, SEEK_CUR)

<p>Move the cursor 4 characters backward from the current position.</p> Signup and view all the answers

What function is used to read a single character from a file?

<p>fgetc (A)</p> Signup and view all the answers

The function fgets reads characters until it encounters EOF.

<p>False (B)</p> Signup and view all the answers

What does the function ftell do in file handling?

<p>It indicates the current position in the file.</p> Signup and view all the answers

To reposition the cursor to the beginning of a file, you can use the function ______.

<p>rewind</p> Signup and view all the answers

Match the following file handling functions with their descriptions:

<p>fgetc = Reads a single character fgets = Reads a string from a file fscanf = Reads formatted data from a file fseek = Moves the file cursor to a specific position</p> Signup and view all the answers

What is the purpose of the fscanf function?

<p>Read formatted data from a file (C)</p> Signup and view all the answers

The function fseek is used to determine the current position in a file.

<p>False (B)</p> Signup and view all the answers

What does the function fscanf return when it successfully reads data from a file?

<p>The number of input items successfully matched and assigned.</p> Signup and view all the answers

In file handling, the function ______ is used to read a line of text into a string.

<p>fgets</p> Signup and view all the answers

Which function would you use to move the file cursor back to the beginning of the file?

<p>rewind (C)</p> Signup and view all the answers

What is the role of the compiler in the development process?

<p>Passes a source file to an object file (A)</p> Signup and view all the answers

The gcc is a type of text editor used in Linux.

<p>False (B)</p> Signup and view all the answers

What file extension is used for programs written in C?

<p>.c</p> Signup and view all the answers

The _____ links object files from different modules with the libraries for the application.

<p>linker</p> Signup and view all the answers

Match the following components of system development with their functions:

<p>Text Editor = Write and edit code Compiler = Convert source code to object code Linker = Combine object files into an executable Libraries = Provide reusable code modules</p> Signup and view all the answers

Which one of the following is NOT a component in the system design?

<p>Debugger (D)</p> Signup and view all the answers

The shell is an integral part of the system architecture in UNIX.

<p>True (A)</p> Signup and view all the answers

Name one commonly used text editor in UNIX for writing C programs.

<p>vi</p> Signup and view all the answers

What does fork() do in a process management context?

<p>Creates a new process (D)</p> Signup and view all the answers

If fork() returns -1, it indicates that a new process has been successfully created.

<p>False (B)</p> Signup and view all the answers

What error code does a process receive when it needs to retry its duplicate request?

<p>EAGAIN</p> Signup and view all the answers

In the fork() example, the child process prints ‘CHILD %d’ while the parent process prints ‘______ %d’.

<p>PARENT</p> Signup and view all the answers

Match the programming construct with its description:

<p>fork() = System call to create a new process errno = Holds the error codes for system calls getpid() = Returns the process ID of the calling process getppid() = Returns the parent process ID</p> Signup and view all the answers

In what scenario would a process enter the do-while loop when calling fork()?

<p>When the fork() call fails with errno set to EAGAIN. (C)</p> Signup and view all the answers

A parent process must wait for the child process to finish before it can continue executing.

<p>False (B)</p> Signup and view all the answers

What is printed by the child process if the fork() returns 0?

<p>Fils : PID=..., PPID=...</p> Signup and view all the answers

In the second fork() example, the variable 'pid' is of type ______.

<p>pid_t</p> Signup and view all the answers

Which of the following is an incorrect way to use a for loop in the provided code?

<p>for( i=0; I &lt; 1000; i++ ) printf(‘CHILD %d ’, i); (C), for( i=0; i &lt; 1000, i++ ) printf(‘PARENT %d ’, i); (D)</p> Signup and view all the answers

Flashcards

Unix Kernel

The core of any Unix system, responsible for managing resources and interacting with hardware.

Shell

A command line interpreter used to interact with the Unix kernel, allowing users to execute commands and manage the system.

Libraries

A collection of pre-written code modules that can be used in programs to perform common tasks, providing reusable functionality.

Utilities

Programs designed to perform specific tasks, aiding in system administration, management, and development.

Signup and view all the flashcards

Unix System Design

The process of designing and structuring a Unix system based on distinct layers: the kernel, shell, libraries, and utilities.

Signup and view all the flashcards

Compilation

The process of transforming a program written in a high-level language (like C) into a low-level machine-readable format, capable of being executed by the computer.

Signup and view all the flashcards

Linking

The process of combining different compiled program modules (object files) and linking them with necessary libraries, generating an executable file.

Signup and view all the flashcards

GCC (GNU Compiler Collection)

A powerful compiler designed for the GNU system (including Linux), used for compiling C, C++, and other programming languages.

Signup and view all the flashcards

gcc

A common command on Unix-like systems used to compile programs. Offers different flags to control the compilation process, such as -O for optimization and -Wall for warnings about potential issues.

Signup and view all the flashcards

Code Optimization

The process of modifying the generated program code to make it more efficient and faster. Usually involves optimizing calculations and memory usage, but can increase compilation duration and memory usage.

Signup and view all the flashcards

Warnings (or -Wall flag during compilation)

Extra messages generated by the compiler that highlight possible errors or potential problems in the code. These messages are not errors but hints for the programmer to improve the quality of the code.

Signup and view all the flashcards

Object file

A file containing compiled instructions for a specific module or program. It is a step towards creating a full executable file.

Signup and view all the flashcards

fgetc()

This function allows you to read a single character from a file.

Signup and view all the flashcards

fgets()

This function reads a line from a file, including any whitespace characters.

Signup and view all the flashcards

fscanf()

This function reads formatted data from a file, using placeholders like %d for integers and %s for strings.

Signup and view all the flashcards

ftell()

This function returns the current position of the file pointer within a file.

Signup and view all the flashcards

fseek()

This function sets the file pointer to a specific position within a file.

Signup and view all the flashcards

rewind()

This function resets the file pointer to the beginning of the file.

Signup and view all the flashcards

EOF

This symbol represents the end of a file when reading.

Signup and view all the flashcards

fork()

A system call that creates a new process, which is a copy of the existing process (parent process) in the system.

Signup and view all the flashcards

EAGAIN

The Unix error code indicating that a resource is temporarily unavailable, often due to system limitations.

Signup and view all the flashcards

fork() loop with EAGAIN check

A loop that continuously attempts to fork a new process until it succeeds or encounters a different error than EAGAIN.

Signup and view all the flashcards

PID

The process identifier, a unique number assigned to each process in the system.

Signup and view all the flashcards

PPID

The parent process identifier, the PID of the process that created the current process.

Signup and view all the flashcards

getpid()

A function that returns the current process's identifier (PID).

Signup and view all the flashcards

getppid()

A function that returns the current process's parent process identifier (PPID).

Signup and view all the flashcards

wait()

A system call that allows a process to wait for the termination of another process.

Signup and view all the flashcards

Process request retries

The ability of a process to repeat its request for a resource, typically after encountering a temporary error like EAGAIN.

Signup and view all the flashcards

Fork() Example (prog.c)

A simple example demonstrating the use of fork() to create a child process and distinguish between parent and child processes using PIDs.

Signup and view all the flashcards

wait() Function

A system call that allows a parent process to wait for its child process to complete and retrieve its exit status.

Signup and view all the flashcards

Forking a Process

A technique used to create a new process that is a copy of the calling process. The child process inherits the parent's environment, but can then execute a different program.

Signup and view all the flashcards

execlp() Function

A function that executes a new program in the current process context. It loads the executable file and replaces the current process image.

Signup and view all the flashcards

Raw Files

A specific mode of storing data directly on a disk, using a simple byte-by-byte approach. It allows for quick access and manipulation but requires managing details like data structures and addresses.

Signup and view all the flashcards

Formatted File

This is a type of file that stores its contents in a consistent, structured way, allowing for easier access and management. It is typically organized using a specific data structure and metadata.

Signup and view all the flashcards

Parent Process

A process that initiates and manages the creation of a child process, often responsible for creating resources and handling communication.

Signup and view all the flashcards

Child Process

A process created through the fork() system call, inheriting some characteristics from its parent process.

Signup and view all the flashcards

What is the ftell() function?

The ftell() function determines the current position of the file pointer within a file.

Signup and view all the flashcards

Explain the purpose of the fseek() function.

The fseek() function allows you to reposition the file pointer within a file to a specific location.

Signup and view all the flashcards

What are the different origins of the file pointer in the fseek() function?

The fseek() function uses various origins to determine the starting point for the file pointer's displacement. SEEK_SET indicates the beginning of the file, SEEK_CUR the current position, and SEEK_END defines the end of the file.

Signup and view all the flashcards

What is the purpose of the SEEK_SET constant in the fseek() function?

In the context of the fseek() function, SEEK_SET specifies that the file pointer's displacement should be calculated from the beginning of the file.

Signup and view all the flashcards

What is the purpose of the SEEK_CUR constant in the fseek() function?

The SEEK_CUR constant within the fseek() function signifies that the calculated displacement should originate from the current position of the file pointer.

Signup and view all the flashcards

What is the purpose of the SEEK_END constant in the fseek() function?

The SEEK_END constant in the fseek() function means the displacement should originate from the end of the file.

Signup and view all the flashcards

Explain the purpose of the rewind() function.

The rewind() function in file handling sets the file pointer back to the beginning of the file.

Signup and view all the flashcards

How does rewind() relate to fseek()?

The rewind() function is essentially equivalent to using fseek() to move the file pointer to 0.

Signup and view all the flashcards

Study Notes

Introduction

  • This document contains notes on the presentation of programming systems, specifically focusing on processes.

Processes

  • A process is the memory image of a program currently running. It has a life cycle. It begins, runs, and then ends.
  • A program launched twice creates two distinct processes.
  • Every process is identified in memory by a unique number assigned by the operating system called the PID (Process ID).

Process Tree in a Unix System

  • The init process is the parent of all other processes in a Unix system.
  • init assigns the machine name, initializes the date and time, and ensures the system has been properly shut down.
  • It starts the "getty" process which displays "login" prompt on a terminal and waits for input.
  • It also launches the "inetd" process for network monitoring.
  • Processes ending in "d" are called "daemon" processes that are server processes which work in continuous loops to provide consistent service to clients.

Process States

  • Initial: newly created, in transition.
  • Active: the process is executing.
  • Waiting: the process is paused and relinquishes the CPU to another process.
  • Terminating: the process has finished execution.

Process Concepts

  • A processor executes one task at a time.
  • The process has various states, such as "Ready," "Running," "Blocked," and "Terminated." These states depict the behavior of the process.
  • Transitions happen between states on event occurrence. For instance, a process can transition to 'ready' from 'blocked' or 'terminated' on completion.

Process Creation

  • User processes are launched by a command interpreter (shell). They can also create further processes.
  • Processes need to interact among each other.
  • The creator process is termed a "Parent" process.
  • Processes created are referred to as "Children" processes.

Process Termination

  • Process termination can be done in 3 ways:
  • Normal Termination: Finishing its operations
  • Permitted Termination: By the parent process executing a suitable command
  • Abnormal Termination: By the system, such as exceeding execution time or insufficient memory.

Implementation

  • The operating system maintains a process table, or table of processes, to track individual processes.
  • Each entry contains information about the process, including its status, instruction address, stack pointer, open files, and the allocated memory.

Process Structure

  • An operating system process comprises various elements such as PID (Process ID), UID (User ID), GID (Group ID), open files, maximum file size, and priority.
  • The Terminal is the terminal from which a command was executed.

Priorities

  • Under Linux, processes have a priority of -20 to 19 to control execution order.
  • Higher values indicate lower priority.
  • The nice command is used to adjust process priority

Example: Structure of a Unix Process

  • Processes have unique IDs.
  • Processes have sets of open files and a current directory.
  • Processes have a certain priority, and execution time.

The ps command

  • Shows information about running processes.
  • Provides a list of processes linked to a specific terminal, along with CPU utilisation data.
  • Useful for viewing processes that aren't linked with terminals.

The kill command

  • Sends a signal to a process to perform an action.
  • Accepts a signal number.

Signals

  • A signal in an operating system is an asynchronous event that can interrupt the running of a program.
  • Signals can be generated by hardware events (e.g., a signal to inform a process that an interrupt has occurred), the operating system itself, another process, or an error condition within the process.
  • Example Signals: SIGINT, SIGQUIT, SIGKILL, etc.

Job Management

  • A job is a collection of one or more commands.
  • Commands, including programs, can be made into a job.
  • Launch jobs in the background via the '&' character.
  • The "jobs" command displays all running jobs.
  • Use the 'bg' command to resume the execution of a stopped job.
  • Use the 'fg' command to bring a job into the foreground.

Linux Management of Services

  • The "service" command manipulates machine services.
  • Shows status of all running services.
  • Useful for managing processes.

Concepts and Tools.

  • General information about Linux development.
  • Information for developers on interaction between application and the machine.
  • Information about system calls.

Development in Linux

  • There are three basic steps in Linux development:
    • Editing the source code.
    • Compiling the source code into an executable file.
    • Linking the object files with the required libraries into a single executable file.

Compiling and Linking

  • The compiler/linker (gcc) converts source code into machine language.
  • Different options are used during compilation, like ‘-g’ for debugging and ‘-Wall’ to display warnings.

Execution

  • An example of how to run C code.
  • How to compile, execute programs in Linux.

Example

  • Illustrative examples for various program execution scenarios.

Compilation in C

  • Explains the compilation process in C programs using gcc.

The Makefile

  • Managing multiple source files and dependencies within a C project.
  • Using a makefile to automate compilation for large projects.
  • Ways to write the different rules to automate compilation.
  • Ways to define rules to automate compilation.
  • Ways to define variables within a makefile for reuse.
  • A guide on how to write, read and correctly interpret a makefile.

Parameters to "main()"

  • Explanation of parameters in the main() function in a C program, including argc, argv, and envp.
  • Explains what each parameter represents and how they are used within the program.
  • Gives some example implementations in order to better understand their usage.

The PATH Variable

  • The value of PATH is a set of paths.
  • The executables are searched at all the paths defined within the PATH value.

Access to Environment

  • How to access environment variables in code.
  • Example usage and retrieval of values.

System Calls C

  • Methods for creating and manipulating processes in C.
  • Explanation of the system calls and their use in controlling process behavior.
    • Description of the 'fork' system call.

The fork System Call

  • Usage: Creation of child processes, and how processes can communicate with each other after using fork().

Process Presentation

  • Explains how processes are presented.
  • Clarifies the parent-child relationship between two processes, and illustrates how the system deals with the parent process after the creation of a child process.

The wait System Call

  • How to wait for a child process's status after the fork system call is used.
  • This call is used to ensure the parent process stops till the child process ends, therefore allowing processes to complete in sequence.

Displaying the Date with the Child Process

  • Demonstrates an example of using the fork and execv system calls to run a command, and capture its output to display within the current application.

Implementing Files

  • Describes the various methods for interacting with files.
  • Explains how raw file manipulation can differ from the standard input and output operations.

File Descriptors

  • Discusses the concept of file descriptors for identifying files in a C program
  • Explains the importance of understanding file descriptors in performing file operations.

stat Structure

  • How to access file details using stat.
  • Describes the contents of the struct stat to indicate file information like permissions, creation, modification, and access times.

File Writing

  • Explanation of writing operations on files in C using the write system call.

File Reading

  • Explanation of reading operations on files in C using the read system call.

File Management

  • Various methods for managing and manipulating files in C, including creating, writing, reading, closing, and renaming files.

File Buffering

  • Explains file buffering and its relationship with file I/O operations.

File Operations

  • A summary of file operations and the necessary headers.

Standard POSIX

  • Summary of the POSIX standard and its relevance to file operations.

Named Pipes

  • Explanation of named pipes for inter-process communication.
  • Showing how named pipes can persist across process terminations.

Signal Handling

  • Introduction to signals and how they can be handled in C programs to make applications more robust.
  • Illustrates how signal handling can be managed in processes to handle errors, or to perform some task when an error is encountered.

Signals : CTRL-C & CTRL-Z

  • Illustrates how the CTRL+C and CTRL+Z commands can be used to interrupt a program.

Signal Handling

  • Explains different ways to handle signals.
  • Explanation of how to handle signals including the use of signal(), pause(), and alarm().
  • Explains the structure, role, handling of signals.
    • Includes code examples, to help to understand the different signal handling.

Signal Interface

  • Explains the interface for managing signals in C programs.

Signal Values in Unix

  • Explains the different signal values in Unix-like systems (Linux).

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Programmation Système PDF

More Like This

GCC Quiz
10 questions

GCC Quiz

LuminousShark8753 avatar
LuminousShark8753
GCC Countries Geography
12 questions
GCC Academic Policies Flashcards
33 questions
Use Quizgecko on...
Browser
Browser