0% found this document useful (0 votes)
10 views10 pages

aniketos4

The document outlines an experiment focused on basic Linux commands and system calls, detailing their syntax and functionality for file management and process handling. It includes example code snippets demonstrating the use of system calls like open(), read(), and fork(). The conclusion emphasizes the successful implementation and understanding of these concepts.

Uploaded by

helpd5124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

aniketos4

The document outlines an experiment focused on basic Linux commands and system calls, detailing their syntax and functionality for file management and process handling. It includes example code snippets demonstrating the use of system calls like open(), read(), and fork(). The conclusion emphasizes the successful implementation and understanding of these concepts.

Uploaded by

helpd5124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Date: / /

Roll No: B4-441


Experiment No-04
Aim: To study and implement Basic Linux commands Theory:
Linux Commands:
Linux commands are user-space instructions executed in the terminal to perform operations like file
management, process handling, and system administration. These commands are interpreted by the shell
and often rely on system calls to interact with the kernel.

Command Syntax Description

mkdir mkdir Creates a new directory.


<directory_name>
chdir cd <directory_path> Changes the current directory.

cat cat <filename> Displays the content of a file.

ls ls [options] Lists files and directories.

chown
chown <user>:<group> <file> Changes the ownership of a file.

chmod chmod <permissions> Changes file permissions.


<file>
chgrp chgrp <group> <file> Changes the group ownership of a file.

ps ps [options] Displays currently running processes.

Sorts lines in a file alphabetically or


sort sort <filename> numerically.
grep grep <pattern> <file> Searches for a pattern in a file.

awk awk '{pattern}' <file> A text-processing tool for pattern


scanning.

System Calls:
System calls are low-level functions that allow user programs to request services from the OS kernel,
such as file operations, process management, and memory allocation. These run in kernel space and
provide a bridge between the software and hardware. Common system calls include:
System
Call Syntax Description

int open(const char *path, int flags,


open mode_t mode); Opens a file.

read ssize_t read(int fd, void *buf, size_t Reads data from a file.
count);
ssize_t write(int fd, const void *buf,
write size_t count); Writes data to a file.

close int close(int fd); Closes a file descriptor.

getpid pid_t getpid(void); Returns the process ID of the calling


process.
setpid int setpgid(pid_t pid, pid_t Sets process group ID.
pgid);
getuid uid_t getuid(void); Returns the real user ID of the
calling
System
Call Syntax Description

process.

Returns the real group ID of the


getgid gid_t getgid(void); calling process.
Returns the effective group ID of the
getegid gid_t getegid(void); calling process.
Returns the effective user ID of the
geteuid uid_t geteuid(void); calling process.

CODES:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h> int
main(void)
{ int val; printf("process id is %d\n",getpid());
printf("the real user id is %d\n",getuid());
printf("the effective user id is %d\n",geteuid());
printf("the effective group id is %d\n",getegid());
if((val=fork())<0)
{
printf("problem in creating process");
return 1; }
if(val==0)
{ printf("process id is %d\n",getpid());
printf("the real user id is %d\n",getuid());
printf("the effective user id is
%d\n",geteuid()); printf("the effective
group id is %d\n",getegid()); }
}

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> int
main(void)
{ int
n,fd;
char buff[50];
fd=open("test1.txt",O_RDONLY);
printf("the file descriptor for the file is %d\n",fd);
n=read(fd,buff,10); write(1,buff,n);
}

include<stdio.h> #include<unistd.h>
#include<fcntl.h>
int main(int argc,char const *argv[])
{
char name[50],buffer[50];
int fd=open("pb.txt",O_RDWR|O_CREAT);
if(fd!=1) {
printf("enter your name");
fgets(name,50,stdin);
write(fd,name,50);
lseek(fd,0,0); read(fd,buffer,50);
printf("data read from the file is :%s\n",buffer);
close(fd);
} else
{
printf("error!file not open\n");
} close(fd);
return 0;
}

CONCLUSION:
we successfully implement and understand basic Linux commands and system calls related file
management like open(),read() and process management related fork() etc.

You might also like