SlideShare a Scribd company logo
Lab	
  exercise-­‐	
  Using	
  Linux	
  O/S.	
  
Instruction:
1.  Create p01hw.c c program
2.  Save in myc directory
3.  Compile by using $gcc p01hw.c (if there is no syntax
error, an executable file a.out will be created)
4.  $mv a.out p01hw.exe  rename
5.  Run the executable file by issuing p01hw.exe at the $
prompt
p01hw.c
#include <stdio.h>
main()
{
printf(“n Hello World nn”);
}
After successfully create and execute the first
program p01hw.c, you are ready to create, compile
and execute another c programs.
Note:	
  
1.  Login	
  into	
  linux/unix	
  by	
  using	
  username/password	
  given	
  by	
  the	
  technician	
  
2.  Run	
  gedit/text	
  editor	
  to	
  create	
  your	
  first	
  program	
  (recommended,	
  instead	
  of	
  
using	
  vi	
  editor)	
  
3.  Open	
  a	
  terminal	
  if	
  you	
  want	
  to	
  issue	
  a	
  Linux	
  command	
  at	
  the	
  $	
  prompt	
  
For	
  example:	
  	
  (study	
  all	
  these	
  commands)	
  
$PATH=$PATH:.	
  	
  	
  	
  	
  	
  to	
  include	
  current	
  directory	
  	
  for	
  file	
  searching	
  
$echo	
  $PATH	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  to	
  display	
  the	
  content	
  	
  of	
  env.	
  var.	
  PATH	
  
$mkdir	
  myc	
  	
  	
  	
  	
  make	
  directory	
  myc	
  
$cd	
  myc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  change	
  to	
  directory	
  myc	
  
$vi	
  p01hw.c	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  create	
  p01hw.c	
  program	
  using	
  vi	
  editor	
  
	
  	
  	
  	
  	
  In	
  vi:	
  	
  	
  	
  i=insert,	
  	
  <esc>	
  exit	
  to	
  command	
  mode,	
  x=delete	
  a	
  char,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  :wq	
  	
  	
  or	
  	
  ZZ	
  	
  to	
  save	
  and	
  quit	
  	
  OR	
  
$gedit	
  p01hw.c	
  &	
  use	
  gedit	
  to	
  create	
  your	
  program	
  (run	
  at	
  the	
  background)	
  
$ls	
  	
  –l	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  to	
  display	
  the	
  content	
  of	
  current	
  directory	
  
$ps	
  	
  –ef	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  to	
  display	
  process	
  status	
  /	
  running	
  programs	
  
$kill	
  	
  -­‐9	
  nnnn	
  	
  	
  	
  	
  to	
  kill	
  a	
  process	
  with	
  a	
  pid=nnnn	
  
Use gedit/text editor to edit a file
p02hw.c
#include <stdio.h>
main()
{
int i, j=11;
for(i=0; i < j; i++)
printf(“n%d Hello Worldnn”, i);
}
p03fork.c
#include <stdio.h>
#include <unistd.h>
main()
{
int i;
i = fork();
printf(“Hello World %d %dn”, i, getpid());
} // getpid() is to display process id (pid)
// getppid() is to display parent’s pid
p04fork.c
#include <stdio.h>
#include <unistd.h>
main()
{
printf(“nt Hello Worldnn”);
fork();
printf(“nt Peace!nn”);
fork();
printf(“nt Bye bye Worldnn”);
}
Note: Analyze the output!
March 2014
Lab	
  exercise@KPU	
  
p05hw.c
#include <stdio.h>
#include <unistd.h>
main()
{
int i=1;
while (i != 0)
printf(“n%d Hello %dn”, getpid(),i++);
}
ü  Use CTRL-C to interrupt a running program!
ü  Based on the pid, use kill to terminate the
process.
p06fork.c
#include <stdio.h>
#include <unistd.h>
main()
{
int pid1, pid2, pid3;
printf(“nHello Worldnn”);
pid1=fork();
pid2=fork();
printf(“nWorld! %d %dnn”, pid1, pid2);
pid3=fork();
printf(“nBye bye World %dnn”, getpid());
}
p07fork.c
#include <stdio.h>
#include <unistd.h>
main()
{
int pid1, pid2;
printf(“nHello Worldnn”);
pid1 = fork();
if (pid1 < 0)
printf(“nBye bye Worldnn”);
else if (pid1 == 0)
printf(“nI’m the childnn”);
else {
pid2 = fork();
printf(“nI’m the parent/another childnn”, pid2);}
}
p08fork.c
#include <stdio.h>
#include <unistd.h>
main()
{
int pid1;
printf(“nHello Worldnn”);
pid1 = fork();
if (pid1 < 0)
printf(“nBye bye Worldnn”);
else if (pid1 == 0)
{
sleep(5);  sleep for 5 seconds
printf(“nChild processnn”);
}
else
{
usleep(2000000);  sleep for 2 seconds
printf(“nParent processnn”);
}
}  Parent will wake-up first
Use gcc to compile a c++ program March 2014
Lab	
  exercise@KPU	
  
p08fork2.c
#include <stdio.h>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid, cpid,ppid,gppid;
pid=fork();
if (pid < 0) {
printf(" Bye bye World n");
exit(1);}
else if (pid == 0) {
ppid=getppid();
sleep(5);
printf(“Child process %d, parent %dn",cpid=getpid(),ppid);}
else {
gppid=getppid();
usleep(2000000);
printf(" parent process %d grandParent %dn",cpid=getpid(),gppid);}
return 0;
}
Use gcc to compile a c++ program March 2014

More Related Content

PDF
Functional php
PDF
Git avançado
DOCX
PPTX
Ensemble: A DSL for Concurrency, Adaptability, and Distribution
DOCX
basic programs in C++
DOC
basic shell_programs
PDF
Controlling Arduino With PHP
PPTX
01 คำสั่งแสดงผล ในภาษาซี
Functional php
Git avançado
Ensemble: A DSL for Concurrency, Adaptability, and Distribution
basic programs in C++
basic shell_programs
Controlling Arduino With PHP
01 คำสั่งแสดงผล ในภาษาซี

What's hot (20)

PPTX
Session07 recursion
PDF
PPTX
งานนำเสนอ อาจารย์ลาวัลย์
PDF
プログラム実行の話と
OSとメモリの挙動の話
PPTX
Cis 216 – shell scripting
PDF
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
DOCX
Pratik Bakane C++
TXT
Problemas de Arreglos en c++
KEY
Ruby haskell extension
KEY
PHPerのためのPerl入門@ Kansai.pm#12
PDF
Code that gets you pwn(s|'d)
PDF
C++ programs
PDF
Fragmentation
DOCX
Tugas Program C++
TXT
Yg byev2e
PDF
Php arduino
PDF
From Javascript To Haskell
PDF
Sistemas operacionais 13
PDF
Shell Script
Session07 recursion
งานนำเสนอ อาจารย์ลาวัลย์
プログラム実行の話と
OSとメモリの挙動の話
Cis 216 – shell scripting
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Pratik Bakane C++
Problemas de Arreglos en c++
Ruby haskell extension
PHPerのためのPerl入門@ Kansai.pm#12
Code that gets you pwn(s|'d)
C++ programs
Fragmentation
Tugas Program C++
Yg byev2e
Php arduino
From Javascript To Haskell
Sistemas operacionais 13
Shell Script
Ad

Viewers also liked (8)

PDF
JSONでメール送信(Haineko) / KOF2013
PPT
Možnosti optimalizácie akciového screenera Piotroski a vplyv optimalizácie na...
PPT
Buxus day 2010 - Prístupnosť webov
PDF
Perlの書籍紹介/KOF2013
PPT
O que não fazer na Produção de Conteúdo
PDF
Brochure pooq zzp_1104
PDF
JSONでメール送信 | HTTP API Server ``Haineko''/YAPC::Asia Tokyo 2013 LT Day2
PPT
Redação para audiovisual
JSONでメール送信(Haineko) / KOF2013
Možnosti optimalizácie akciového screenera Piotroski a vplyv optimalizácie na...
Buxus day 2010 - Prístupnosť webov
Perlの書籍紹介/KOF2013
O que não fazer na Produção de Conteúdo
Brochure pooq zzp_1104
JSONでメール送信 | HTTP API Server ``Haineko''/YAPC::Asia Tokyo 2013 LT Day2
Redação para audiovisual
Ad

Similar to Fork handout (20)

PDF
Os lab final
PPTX
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
PPT
Linux_C_LabBasics.ppt
DOCX
CS3451-INTRODUCTION TO OPERATING SYSTEM-91035556-OS LAB CSE (1).docx
PDF
Using Flow-based programming to write tools and workflows for Scientific Comp...
PPTX
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
DOCX
Program Assignment Process ManagementObjective This program a.docx
PPT
Unix And C
PDF
Ch02.pdf
DOCX
CS3451-INTRODUCTION TO OPERATING SYSTEM-91035556-OS LAB CSE.docx
PPTX
C - ISRO
DOCX
Design problem
PPTX
Lecturer notes on file handling in programming C
PDF
Virus lab
PDF
Advanced debugging  techniques in different environments
PDF
Bash Scripting
PPTX
Introduction to pcDuino
PDF
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
Os lab final
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux_C_LabBasics.ppt
CS3451-INTRODUCTION TO OPERATING SYSTEM-91035556-OS LAB CSE (1).docx
Using Flow-based programming to write tools and workflows for Scientific Comp...
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
Program Assignment Process ManagementObjective This program a.docx
Unix And C
Ch02.pdf
CS3451-INTRODUCTION TO OPERATING SYSTEM-91035556-OS LAB CSE.docx
C - ISRO
Design problem
Lecturer notes on file handling in programming C
Virus lab
Advanced debugging  techniques in different environments
Bash Scripting
Introduction to pcDuino
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift

Recently uploaded (20)

PDF
The Land of Punt — A research by Dhani Irwanto
PPTX
Pharmacology of Autonomic nervous system
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
The Minerals for Earth and Life Science SHS.pptx
PPT
veterinary parasitology ````````````.ppt
PDF
Phytochemical Investigation of Miliusa longipes.pdf
PPTX
Application of enzymes in medicine (2).pptx
PPTX
Science Quipper for lesson in grade 8 Matatag Curriculum
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PDF
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
PDF
Assessment of environmental effects of quarrying in Kitengela subcountyof Kaj...
PPTX
Introcution to Microbes Burton's Biology for the Health
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PDF
CHAPTER 3 Cell Structures and Their Functions Lecture Outline.pdf
PDF
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
PDF
An interstellar mission to test astrophysical black holes
PDF
BET Eukaryotic signal Transduction BET Eukaryotic signal Transduction.pdf
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PPTX
CORDINATION COMPOUND AND ITS APPLICATIONS
PDF
GROUP 2 ORIGINAL PPT. pdf Hhfiwhwifhww0ojuwoadwsfjofjwsofjw
The Land of Punt — A research by Dhani Irwanto
Pharmacology of Autonomic nervous system
7. General Toxicologyfor clinical phrmacy.pptx
The Minerals for Earth and Life Science SHS.pptx
veterinary parasitology ````````````.ppt
Phytochemical Investigation of Miliusa longipes.pdf
Application of enzymes in medicine (2).pptx
Science Quipper for lesson in grade 8 Matatag Curriculum
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
Assessment of environmental effects of quarrying in Kitengela subcountyof Kaj...
Introcution to Microbes Burton's Biology for the Health
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
CHAPTER 3 Cell Structures and Their Functions Lecture Outline.pdf
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
An interstellar mission to test astrophysical black holes
BET Eukaryotic signal Transduction BET Eukaryotic signal Transduction.pdf
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
CORDINATION COMPOUND AND ITS APPLICATIONS
GROUP 2 ORIGINAL PPT. pdf Hhfiwhwifhww0ojuwoadwsfjofjwsofjw

Fork handout

  • 1. Lab  exercise-­‐  Using  Linux  O/S.   Instruction: 1.  Create p01hw.c c program 2.  Save in myc directory 3.  Compile by using $gcc p01hw.c (if there is no syntax error, an executable file a.out will be created) 4.  $mv a.out p01hw.exe rename 5.  Run the executable file by issuing p01hw.exe at the $ prompt p01hw.c #include <stdio.h> main() { printf(“n Hello World nn”); } After successfully create and execute the first program p01hw.c, you are ready to create, compile and execute another c programs. Note:   1.  Login  into  linux/unix  by  using  username/password  given  by  the  technician   2.  Run  gedit/text  editor  to  create  your  first  program  (recommended,  instead  of   using  vi  editor)   3.  Open  a  terminal  if  you  want  to  issue  a  Linux  command  at  the  $  prompt   For  example:    (study  all  these  commands)   $PATH=$PATH:.            to  include  current  directory    for  file  searching   $echo  $PATH                        to  display  the  content    of  env.  var.  PATH   $mkdir  myc          make  directory  myc   $cd  myc                        change  to  directory  myc   $vi  p01hw.c                    create  p01hw.c  program  using  vi  editor            In  vi:        i=insert,    <esc>  exit  to  command  mode,  x=delete  a  char,                                      :wq      or    ZZ    to  save  and  quit    OR   $gedit  p01hw.c  &  use  gedit  to  create  your  program  (run  at  the  background)   $ls    –l                                      to  display  the  content  of  current  directory   $ps    –ef                              to  display  process  status  /  running  programs   $kill    -­‐9  nnnn          to  kill  a  process  with  a  pid=nnnn   Use gedit/text editor to edit a file p02hw.c #include <stdio.h> main() { int i, j=11; for(i=0; i < j; i++) printf(“n%d Hello Worldnn”, i); } p03fork.c #include <stdio.h> #include <unistd.h> main() { int i; i = fork(); printf(“Hello World %d %dn”, i, getpid()); } // getpid() is to display process id (pid) // getppid() is to display parent’s pid p04fork.c #include <stdio.h> #include <unistd.h> main() { printf(“nt Hello Worldnn”); fork(); printf(“nt Peace!nn”); fork(); printf(“nt Bye bye Worldnn”); } Note: Analyze the output! March 2014
  • 2. Lab  exercise@KPU   p05hw.c #include <stdio.h> #include <unistd.h> main() { int i=1; while (i != 0) printf(“n%d Hello %dn”, getpid(),i++); } ü  Use CTRL-C to interrupt a running program! ü  Based on the pid, use kill to terminate the process. p06fork.c #include <stdio.h> #include <unistd.h> main() { int pid1, pid2, pid3; printf(“nHello Worldnn”); pid1=fork(); pid2=fork(); printf(“nWorld! %d %dnn”, pid1, pid2); pid3=fork(); printf(“nBye bye World %dnn”, getpid()); } p07fork.c #include <stdio.h> #include <unistd.h> main() { int pid1, pid2; printf(“nHello Worldnn”); pid1 = fork(); if (pid1 < 0) printf(“nBye bye Worldnn”); else if (pid1 == 0) printf(“nI’m the childnn”); else { pid2 = fork(); printf(“nI’m the parent/another childnn”, pid2);} } p08fork.c #include <stdio.h> #include <unistd.h> main() { int pid1; printf(“nHello Worldnn”); pid1 = fork(); if (pid1 < 0) printf(“nBye bye Worldnn”); else if (pid1 == 0) { sleep(5); sleep for 5 seconds printf(“nChild processnn”); } else { usleep(2000000); sleep for 2 seconds printf(“nParent processnn”); } } Parent will wake-up first Use gcc to compile a c++ program March 2014
  • 3. Lab  exercise@KPU   p08fork2.c #include <stdio.h> #include <math.h> #include <sys/types.h> #include <unistd.h> int main() { int pid, cpid,ppid,gppid; pid=fork(); if (pid < 0) { printf(" Bye bye World n"); exit(1);} else if (pid == 0) { ppid=getppid(); sleep(5); printf(“Child process %d, parent %dn",cpid=getpid(),ppid);} else { gppid=getppid(); usleep(2000000); printf(" parent process %d grandParent %dn",cpid=getpid(),gppid);} return 0; } Use gcc to compile a c++ program March 2014