Unix Manual
Unix Manual
// Check and print the max number of child processes while ((bytes_read = read(source_fd, buffer, sizeof(buffer))) > 0) {
printf("Max. no. of child processes: %ld\n", sysconf(_SC_CHILD_MAX)); ssize_t bytes_written = write(dest_fd, buffer, bytes_read);
if (bytes_written != bytes_read) {
// Check and print the max path length perror("Error writing to destination file");
printf("Max. path length: %ld\n", pathconf("/", _PC_PATH_MAX)); close(source_fd);
close(dest_fd);
// Check and print the max number of characters in a file name return 1;
printf("Max. no. of characters in a file name: %ld\n", pathconf("/", _PC_NAME_MAX)); }
}
// Check and print the max number of open files per process
printf("Max. no. of open files/process: %ld\n", sysconf(_SC_OPEN_MAX)); // Close file descriptors
close(source_fd);
return 0; close(dest_fd);
}
std::cout << "File copied successfully!" << std::endl;
Output :
return 0;
}
#include <iostream>
int main() {
const char* sourceFile = "[Link]";
const char* destinationFile = "[Link]";
if (child_pid == -1) {
std::cerr << "Fork failed." << std::endl;
return 1;
}
return 0;
}
3. a. Emulate the UNIX ln command
b. Create a child from parent process using fork() and counter counts till 5 in both processes and displays.
a.
#include <iostream>
#include <unistd.h>
if (i == limit - 1)
i = 0;
else if (arrival_time[i + 1] <= total)
i++;
else
total++;
}
processes[0].waitingTime = 0;
processes[0].turnaroundTime = processes[0].burstTime;
double totalWaitingTime = 0;
double totalTurnaroundTime = 0;
7. Implement priority-based scheduling algorithm and calculates average waiting time and average turnaround time cout << "\nAverage Waiting Time: " << averageWaitingTime << endl;
cout << "Average Turnaround Time: " << averageTurnaroundTime << endl;
#include <iostream> return 0;
#include <vector> }
#include <algorithm>
struct Process {
int processID;
int burstTime;
int priority;
int waitingTime;
int turnaroundTime;
};
int main() { 8. Act as sender to send data in message queues and receiver that reads data from message queue.
int numProcesses;
cout << "Enter the number of processes: ";
cin >> numProcesses; [Link]
#include <iostream>
vector<Process> processes(numProcesses); #include <cstring>
#include <cstdlib>
for (int i = 0; i < numProcesses; i++) { #include <unistd.h>
#include <sys/types.h> struct Message {
#include <sys/ipc.h> long mtype;
#include <sys/msg.h> char mtext[100];
};
using namespace std;
int main() {
// Define a structure for the message data key_t key;
struct Message { int msgid;
long mtype; Message message;
char mtext[100];
}; // Step 1: Create a key for the message queue (use the same key as in the sender)
key = ftok("/tmp", '1');
int main() { if (key == -1) {
key_t key; perror("ftok");
int msgid; exit(1);
Message message; }
// Step 1: Create a key for the message queue // Step 2: Create or open the message queue
key = ftok("/tmp", '1'); msgid = msgget(key, 0666 | IPC_CREAT);
if (key == -1) { if (msgid == -1) {
perror("ftok"); perror("msgget");
exit(1); exit(1);
} }
// Step 2: Create or open the message queue // Receiver: Read data from the message queue
msgid = msgget(key, 0666 | IPC_CREAT); // Step 3: Receive a message from the queue with message type 1
if (msgid == -1) { if (msgrcv(msgid, &message, sizeof([Link]), 1, 0) == -1) {
perror("msgget"); perror("msgrcv");
exit(1); exit(1);
} }
// Sender: Send data to the message queue cout << "Data received gmessage queue: " << [Link] << endl;
[Link] = 1; // Message type (you can use different types for different purposes)
strcpy([Link], "Hello, this is a message from the sender!"); return 0;
}
// Step 3: Send the message to the queue
if (msgsnd(msgid, &message, sizeof([Link]), 0) == -1) { Output :
perror("msgsnd");
exit(1);
}
9. Where a parent writes a message to pipe and child reads message from pipe
cout << "Data sent to message queue." << endl;
// Create a pipe
[Link] if (pipe(pipe_fd) == -1) {
#include <iostream> perror("Pipe creation failed");
#include <cstring> return 1;
#include <cstdlib> }
#include <unistd.h>
#include <sys/types.h> pid_t child_pid = fork(); // Fork a child process
#include <sys/ipc.h>
#include <sys/msg.h> if (child_pid == -1) {
perror("Fork failed");
using namespace std; return 1;
}
// Define a structure for the message data
if (child_pid > 0) { // Parent process std::cerr << "Error creating server socket" << std::endl;
close(pipe_fd[0]); // Close the read end in the parent return -1;
}
std::string message = "Hello from parent!";
sockaddr_in server_address{};
// Write the message to the pipe server_address.sin_family = AF_INET;
if (write(pipe_fd[1], message.c_str(), [Link]()) == -1) { server_address.sin_addr.s_addr = INADDR_ANY;
perror("Write to pipe failed"); server_address.sin_port = htons(PORT);
return 1;
} if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) {
std::cerr << "Error binding to port " << PORT << std::endl;
close(pipe_fd[1]); // Close the write end in the parent close(server_socket);
} else { // Child process return -1;
close(pipe_fd[1]); // Close the write end in the child }
if (bytes_read == -1) { std::cout << "Server is listening on port " << PORT << std::endl;
perror("Read from pipe failed");
return 1; while (true) {
} sockaddr_in client_address{};
socklen_t client_address_len = sizeof(client_address);
buffer[bytes_read] = '\0'; // Null-terminate the string
int client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_len);
std::cout << "Child process received message: " << buffer << std::endl; if (client_socket == -1) {
std::cerr << "Error accepting connection" << std::endl;
close(pipe_fd[0]); // Close the read end in the child continue;
} }
return 0; handle_request(client_socket);
} }
close(server_socket);
return 0;
}
10. Demonstrate setting up a simple web server and host website on your own Linux computer
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
const int numThreads = 2;
pthread_t threads[numThreads];
if (threadCreateStatus) {
std::cerr << "Error creating thread: " << threadCreateStatus << std::endl;
return -1;
}
}
return 0;
}
11. a. Create two threads using pthread, where both thread counts until 100 and joins later.
b. Create two threads using pthreads. Here, main thread creates 5 other threads for 5
times and each new thread print “Hello World” message with its thread number
b.
#include <iostream>
#include <pthread.h>
a.
#include <iostream> // Function that will be executed by each thread
#include <pthread.h> void* printHello(void* threadNumber) {
int* num = static_cast<int*>(threadNumber);
// Function that will be executed by each thread std::cout << "Hello World from Thread " << *num << std::endl;
void* countTo100(void* arg) { pthread_exit(NULL);
int item = *((int*)arg); }
return 0;
}
//server console