0% found this document useful (0 votes)
40 views14 pages

Cycle 1 Os Lab

Uploaded by

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

Cycle 1 Os Lab

Uploaded by

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

CYCLE-1

S. No LIST OF EXPERIMENTS
Exp-1 Practicing of Basic UNIX Commands.
Exp-2 Write programs using the following UNIX operating system calls
fork, exec, getpid, exit, wait, close, stat, opendir and readdir
Exp-3 Simulate UNIX commands like cp, ls, grep, etc.,
Basic Unix Commands

Getting help in Unix

 man – view manual pages for Unix commands

Unix Shell Commands

 clear – clear screen


 history – show history of previous commands

Time and Date commands

 date – show current date and time


 sleep – wait for a given number of seconds
 uptime – find out how long the system has been up

Unix users commands


These commands allow you to get basic information about Unix users in your
environment.

 whoami – show your username


 id – print user identity
 groups – show which groups user belongs to
 passwd – change user password
 who – find out who is logged into the system
 last – show history of logins into the system

Unix file operations


Navigating filesystem and managing files and access permissions:

 ls – list files and directories


 cp – copy files (work in progress)
 rm – remove files and directories (work in progress)
 mv – move or rename files and directories to another location
 chmod – change file/directory access permissions
 chown – change file/directory ownership

Text file operations in Unix


Most of important configuration in Unix is in clear text files, these commands will let you
quickly inspect files or view logs:

 cat – concatenate files and show contents to the standard output


 more – basic pagination when viewing text files or parsing Unix commands output
 less – an improved pagination tool for viewing text files (better than more command)
 head – show the first 10 lines of text file (you can specify any number of lines)
 tail – show the last 10 lines of text file (any number can be specified)
 grep – search for patterns in text files

Unix directory management commands


Navigating filesystems and managing directories:

 cd – change directory
 pwd – confirm current directory
 ln – make links and symlinks to files and directories
 mkdir – make new directory
 rmdir – remove directories in Unix

Unix system status commands

Most useful commands for reviewing hostname configuration and vital stats:

 hostname – show or set server hostname


 w – display system load, who’s logged in and what they are doing
 uname – print Unix system information

Reboot

 shutdown – graceful shutdown and reboot of your system


 halt – ungraceful (without stopping OS services) shutdown
 reboot – ungraceful reboot (without stopping OS services)

Networking commands in Unix

Most useful commands for inspecting network setup and exploring network connections and
ports:

 ifconfig – show and set IP addresses (found almost everywhere)


 ip – show and set IP addresses (in recent Linux versions)
 ping – check if remote host is reachable via ICMP ping
 netstat – show network stats and routing information

Process management

Listing processes and confirming their status, and stopping processes if needed:

 ps – list processes
 top – show tasks and system status (check out htop as well)
 kill – kill a process (stop application running)

Remote access commands

ssh is really the only way to go, but it’s important to know telnet as well:

 telnet – clear-text (insecure) remote access protocol


 ssh – Secure SHell – encrypted remote access client
o check out the SSH reference!

File transfers commands

Always useful to know how to copy files between servers or just download some package
from the web:

 ftp – clear-text (insecure!) File Transfer Protocol client


 sftp – secure (encrypted) version of FTP
 scp – secure (encrypted) version of cp command
 wget – download files from remote servers, HTTP/HTTPS and FTP
 curl – download files from remote servers, HTTP/HTTPS and FTP

1.File and Directory Management

1. pwd
o Prints the current working directory.
o Example:

bash
Copy code
pwd

2. ls
o Lists files and directories in the current directory.
o Options:
 -l: Long format (permissions, owner, size, etc.).
 -a: Includes hidden files.
o Example:

bash
Copy code
ls -la

3. cd
o Changes the directory.
o Example:

bash
Copy code
cd /path/to/directory
4. mkdir
o Creates a new directory.
o Example:

bash
Copy code
mkdir new_directory

5. rmdir
o Removes an empty directory.
o Example:

bash
Copy code
rmdir empty_directory

6. touch
o Creates an empty file.
o Example:

bash
Copy code
touch file.txt

7. rm
o Removes files or directories.
o Options:
 -r: Recursively delete directories.
o Example:

bash
Copy code
rm file.txt
rm -r directory_name

8. cp
o Copies files or directories.
o Example:

bash
Copy code
cp file.txt /path/to/destination

9. mv
o Moves or renames files or directories.
o Example:

bash
Copy code
mv file.txt /path/to/destination
mv old_name.txt new_name.txt

File Viewing

1. cat
o Displays the contents of a file.
o Example:

bash
Copy code
cat file.txt

2. less
o Allows scrolling through a file's contents.
o Example:

bash
Copy code
less file.txt

3. head
o Displays the first few lines of a file.
o Example:

bash
Copy code
head file.txt

4. tail
o Displays the last few lines of a file.
o Example:

bash
Copy code
tail file.txt

5. nano, vim, or vi
o Opens text editors directly from the command line.

File Permissions

1. chmod
o Changes file permissions.
o Example:

bash
Copy code
chmod 755 script.sh

2. chown
o Changes file ownership.
o Example:

bash
Copy code
chown user:group file.txt

Process Management

1. ps
o Lists running processes.
o Example:

bash
Copy code
ps aux

2. kill
o Terminates a process by its PID.
o Example:

bash
Copy code
kill 12345

3. top
o Displays real-time system usage and processes.
o Example:

bash
Copy code
top

Networking

1. ping
o Tests connectivity to a host.
o Example:

bash
Copy code
ping google.com

2. ifconfig or ip a
o Displays network interface information.
o Example:

bash
Copy code
ip a

Searching

1. find
o Searches for files in a directory hierarchy.
o Example:

bash
Copy code
find /path/to/search -name "file.txt"

2. grep
o Searches for patterns in files.
o Example:

bash
Copy code
grep "search_term" file.txt

System Information

1. uname
o Displays system information.
o Example:

bash
Copy code
uname -a

2. df
o Displays disk space usage.
o Example:

bash
Copy code
df -h

3. du
o Displays disk usage for files and directories.
o Example:

bash
Copy code
du -sh /path/to/directory

4. whoami
o Displays the current user.
o Example:

bash
Copy code
whoami

Practice Tips

 Use a sandbox or virtual machine to practice without risking your main system.
 Try combining commands with pipes (|) to perform complex tasks.
Example:

bash
Copy code
ls -l | grep "filename"

2.1. fork and getpid

A program to create a child process and display process IDs.

c
Copy code
#include <stdio.h>
#include <unistd.h>

int main() {
pid_t pid = fork(); // Create a child process

if (pid < 0) {
perror("Fork failed");
} else if (pid == 0) {
printf("Child process: PID = %d\n", getpid());
} else {
printf("Parent process: PID = %d, Child PID = %d\n", getpid(), pid);
}

return 0;
}

2. exec
A program to replace the current process with a new one.

c
Copy code
#include <stdio.h>
#include <unistd.h>

int main() {
printf("Executing `ls` command:\n");
execl("/bin/ls", "ls", "-l", (char *)NULL); // Replace current process with `ls`
perror("execl failed");
return 1;
}

3. exit and wait

A program where the child exits and the parent waits.

c
Copy code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
pid_t pid = fork();

if (pid < 0) {
perror("Fork failed");
} else if (pid == 0) {
printf("Child process exiting with code 42\n");
exit(42); // Exit with a status code
} else {
int status;
wait(&status); // Parent waits for the child
if (WIFEXITED(status)) {
printf("Child exited with status: %d\n", WEXITSTATUS(status));
}
}

return 0;
}

4. close

A program to open, write to, and close a file descriptor.


c
Copy code
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
int fd = open("example.txt", O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
perror("Open failed");
return 1;
}

write(fd, "Hello, UNIX!\n", 13); // Write to the file


close(fd); // Close the file descriptor
printf("File written and closed successfully.\n");

return 0;
}

5. stat

A program to retrieve and display file information.

c
Copy code
#include <stdio.h>
#include <sys/stat.h>

int main() {
struct stat fileStat;
if (stat("example.txt", &fileStat) < 0) {
perror("stat failed");
return 1;
}

printf("File Size: %ld bytes\n", fileStat.st_size);


printf("File Permissions: %o\n", fileStat.st_mode & 0777);
printf("Last Access Time: %ld\n", fileStat.st_atime);

return 0;
}

6. opendir and readdir

A program to list all files in a directory.

c
Copy code
#include <stdio.h>
#include <dirent.h>

int main() {
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir failed");
return 1;
}

struct dirent *entry;


printf("Files in current directory:\n");
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

closedir(dir); // Close the directory stream


return 0;
}

Compilation and Execution

1. Save the code to a file, e.g., program.c.


2. Compile with gcc:

bash
Copy code
gcc program.c -o program

3. Run the program:

bash
Copy code
./program

Let me know if you need additional explanations or variations of these examples!

4o

3.

1. Simulate cp (Copy File)

c
Copy code
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
return 1;
}

int src = open(argv[1], O_RDONLY);


if (src < 0) {
perror("Source file open failed");
return 1;
}

int dest = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);


if (dest < 0) {
perror("Destination file open failed");
close(src);
return 1;
}

char buffer[1024];
ssize_t bytesRead, bytesWritten;
while ((bytesRead = read(src, buffer, sizeof(buffer))) > 0) {
bytesWritten = write(dest, buffer, bytesRead);
if (bytesWritten != bytesRead) {
perror("Write error");
close(src);
close(dest);
return 1;
}
}

close(src);
close(dest);
printf("File copied successfully.\n");
return 0;
}

2. Simulate ls (List Files in Directory)

c
Copy code
#include <stdio.h>
#include <dirent.h>

int main(int argc, char *argv[]) {


const char *path = (argc == 2) ? argv[1] : ".";
DIR *dir = opendir(path);
if (dir == NULL) {
perror("Failed to open directory");
return 1;
}

struct dirent *entry;


while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

closedir(dir);
return 0;
}

3. Simulate grep (Search for Pattern in File)

c
Copy code
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <pattern> <file>\n", argv[0]);
return 1;
}

const char *pattern = argv[1];


const char *filename = argv[2];
FILE *file = fopen(filename, "r");

if (file == NULL) {
perror("File open failed");
return 1;
}

char line[1024];
while (fgets(line, sizeof(line), file)) {
if (strstr(line, pattern)) { // Check if pattern exists in the line
printf("%s", line);
}
}

fclose(file);
return 0;
}

Compilation and Execution


1. Save the programs to files, e.g., cp_sim.c, ls_sim.c, and grep_sim.c.
2. Compile using gcc:

bash
Copy code
gcc cp_sim.c -o cp_sim
gcc ls_sim.c -o ls_sim
gcc grep_sim.c -o grep_sim

3. Run the programs:


o For cp:

bash
Copy code
./cp_sim source.txt destination.txt

o For ls:

bash
Copy code
./ls_sim /path/to/directory

o For grep:

bash
Copy code
./grep_sim pattern file.txt

These simple programs mimic the basic functionality of the respective UNIX commands.
You can extend them by adding more options and error handling for a closer simulation of
the real commands. Let me know if you need any further assistance!

4o

You might also like