C Programming Fundamentals Quiz
51 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

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

    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</p> Signup and view all the answers

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

    <p>True</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</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</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.</p> Signup and view all the answers

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

    <p>False</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</p> Signup and view all the answers

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

    <p>False</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.</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</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</p> Signup and view all the answers

    The function fgets reads characters until it encounters EOF.

    <p>False</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</p> Signup and view all the answers

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

    <p>False</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</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</p> Signup and view all the answers

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

    <p>False</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</p> Signup and view all the answers

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

    <p>True</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</p> Signup and view all the answers

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

    <p>False</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.</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</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);</p> Signup and view all the answers

    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

    Description

    Test your knowledge on C programming concepts, including compilation commands and process handling. This quiz covers essential gcc options, file generation, and process management functions like wait and fork. Perfect for beginners looking to solidify their understanding of C programming basics.

    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