Interacting with Unix

Similar documents
SYSTEM INFORMATION. UNIX Programming 2015 Fall by Euiseong Seo

Examination C++ Programming

Overview of Time Related Data Structures and Functions in Unix/C. It s About Time

Preview. Review. System Data Files (Password File) System Data Files (Password File) System Data Files (Password File)

Outline. Computer programming. Usage of time and date functions. Date and Time: time.h. Date and Time: time.h. Time functions:

Computer programming. "An expert is a man who has made all the mistakes which can be made, in a narrow field." Niels Bohr

Fall 2015 COMP Operating Systems. Lab #3

Systems Programming/ C and UNIX

CSC 1600 Unix Processes. Goals of This Lecture

CS240: Programming in C

Programming Language B

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Chapter 6. System Data Files and Information

Lesson 2. process id = 1000 text data i = 5 pid = 1200

Operating systems and concurrency - B03

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

System Programming. Process Control III

UNIX System Calls. Sys Calls versus Library Func

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Implementation of Pipe under C in Linux. Tushar B. Kute,

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Parents and Children

This manual is for Libffi, a portable foreign-function interface library. Copyright c 2008, 2010, 2011 Red Hat, Inc. Permission is granted to copy,

CS240: Programming in C

Operating Systems. Processes

File Descriptors and Piping

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Computer Systems Assignment 2: Fork and Threads Package

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Help Session 2. Programming Assignment 2

Process Turnaround Time Total Wait Time P 1 12 ms 0 ms P 2 21 ms 12 ms P 3 23 ms 18 ms P 4 20 ms 17 ms

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

518 Lecture Notes Week 3

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

UNIX Processes. by Armin R. Mikler. 1: Introduction

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Operating systems fundamentals - B06

OS Lab Tutorial 1. Spawning processes Shared memory

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

CS Operating Systems Lab 3: UNIX Processes

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Structs (1) In data processing, a collection of data that can be treated as a single unit is a record. The components of this collection (fields or

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

Operating Systems Lab

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

ACSC 372 Systems Programming. exec() Functions. Outline

CITS2002 Systems Programming. Creating a new process using fork() 1 next CITS2002 CITS2002 schedule

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

Processes. Processes (cont d)

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Compile the Hello World program

Unix Processes 1 / 31

프로세스간통신 (Interprocess communication) i 숙명여대창병모

Programming Assignment #1: A Simple Shell

COMP 3100 Operating Systems

SE350: Operating Systems

CS 550 Operating Systems Spring Process III

tag 220 tan[f l] struct { int i; double d; } sa, sb; struct { int i; double d; } s1, s2;

ปฏ บ ต การ #3. A Simple Shell Interpreter

This tutorial covers a foundational understanding of IPC. Each of the chapters contain related topics with simple and useful examples.

Interprocess Communication E. Im

Process Management! Goals of this Lecture!

Interrupts, Fork, I/O Basics

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

Process Management! Goals of this Lecture!

Process management 1

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

Systems Programming/ C and UNIX

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Chapter 13 Systems Programming. Systems Programming. Error Handling. Error Handling. Example: File I/O. Example: File I/O

Process Creation in UNIX

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Announcement (1) sys.skku.edu is now available

September 2 nd, 2015 Prof. John Kubiatowicz

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Timekeeping. 5February :09

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Process Management 1

Environment Variables

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

System Programming. Pipes I

Creating a Shell or Command Interperter Program CSCI411 Lab

Computer Science & Engineering Department I. I. T. Kharagpur

LECTURE 9 The Standard Library Part 3

Transcription:

Interacting with Unix

Synopsis Getting the Process ID #include <unistd.h> pid_t getpid(void); Example: #include <stdio.h> #include <unistd.h> int main(){ pid_t n = getpid(); printf("process id is %d\n", n);

Getting and Changing the Current Directory SYNOPSIS #include <unistd.h> char *getcwd(char *buf, size_t size); int chdir(const char *path);

Example #include <stdio.h> #include <unistd.h> int main(){ char str[1000]; char*p=getcwd(str,1000); if(p!=str){ printf("could not get cwd!"); exit(1); printf("cwd is %s\n", str); chdir("/usr/bin"); printf("cwd is now %s\n",getcwd(str,1000));

Getting the Current System Time (1) There are a number of library functions relating to time in C. Their prototypes are found in <time.h>. Two data types are the most important for those functions: time_t /* Typically same as long. It is the number of seconds since epoch: 00:00:00 UTC, January 1, 1970 */ struct tm /* See next slide. */ Can go the microsecond or nanosecond accuracy with other structures and functions.

Getting the Current System Time (2) struct tm contains time information broken down: struct tm{ int tm_sec; // seconds [0,61] int tm_min; // minutes [0,59] int tm_hour; // hour [0,23] int tm_mday; // day of month [1,31] int tm_mon; // month of year [0,11] int tm_year; // years since 1900 int tm_wday; // day of week [0,6] (Sunday = 0) int tm_yday; // day of year [0,365] int tm_isdst; // daylight savings flag

Getting the Current System Time (3) Most of the time, you only need the following two functions, but there are others: #include <time.h> time_t time(time_t * time); struct tm *localtime(const time_t * time);

An Example and a Question #include <stdio.h> #include <time.h> int main(){ time_t t = time(null); struct tm * p = localtime(&t); if( p->tm_year >= 102 ){ printf("trial version expired!\n"); exit(0); return 0; /* Question: why don t we free(p)? */

The Answer localtime() looks like the following: struct tm * localtime(const time_t * time){ static struct tm t; t.tm_year = ; return & t; Suggestion: Use man localtime or look up a manual page to find out the exact behavior of a function.

Calling a Command from a C Program In a C program, we can invoke a subshell and let it run a Unix command using the system() function: #include <stdlib.h> int system(const char *); Example: #include <stdio.h> #include <stdlib.h> int main() { int k; printf("files in Directory are: \n"); k = system("ls -l"); printf("%d is returned.\n", k); return k;

Piping to and from Other Programs (1) A command executed by the system() function uses the same standard input and output as the calling program. Sometimes, we want to pipe output from the calling program to the new command, or pipe input from the new command to the calling program. This can be done using the popen() function: #include <stdio.h> FILE *popen(const char *command, const char *mode); int pclose(file *fp); If mode is "r", popen() returns a file pointer that can be used to read the standard output of command. If mode is "w", popen() returns a file pointer that can be used to write to the standard input of command. popen() returns NULL on error.

Piping to and from Other Programs (2) #include<stdio.h> int main() { FILE *fp; char buffer[100]; if ((fp = popen("ls -l", "r"))!= NULL) { while(fgets(buffer, 100, fp)!= NULL) { printf("line from ls:\n"); printf(" %s\n", buffer); pclose(fp); return 0;

execl (1) The system() function returns control to the program it was called from. Immediately, if you background the command with an &. When the command completes, otherwise. Occasionally, you do not want to get the control back. For example, when your program is a loader of another program. execl() is suitable for such purposes. It loads the new program and uses it to replace the current process.

Synopsis #include <unistd.h> execl (2) int execl(const char *path, const char *arg0,..., const char *argn, char * /*NULL*/); path is the pathname of the executable file. arg0 should be the same as path or the filename. arg1 to argn are the actual arguments The last parameter must be NULL (or 0).

Example #include <stdio.h> #include <unistd.h> int main() { printf("files in Directory are:\n"); execl("/bin/ls", "ls, "-l", NULL); printf("this line should not be printed out!\n"); return 0; All statements after execl() will not be executed.

Multi-process Programming With a Unix system, you can write programs that run several processes in parallel. For example, a web-server can invoke child processes, each of which responses to the requests from a different web-browser. We will not get into the detail of this (see CS305a/b). But, we tell you the first step of multi-process programming, so you know where to start.

Synopsis The fork() Function (1) #include <unistd.h> pid_t fork() The fork() function creates a new process. The new process (child process) is an exact copy of the calling process (parent process). The only difference between the child and parent processes is the return value of fork(). Child process gets 0 if fork is successful. Parent gets process id of child or -1 on errors. You can do different things depending on whether it is a child or a parent process.

The fork() Function (2) #include <stdio.h> #include <unistd.h> int main(){ int pid; /* Process identifier */ pid = fork(); if ( pid < 0 ) { printf("cannot fork!!\n"); exit(1); else if ( pid == 0 ) { /* Child process */... else { /* Parent process */...