Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new_change_task #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions README.md

This file was deleted.

Binary file added day2/exec1
Binary file not shown.
21 changes: 21 additions & 0 deletions day2/exec1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<stdio.h>
#include<unistd.h>//process id
#include<stdlib.h>

int main(int argc, char *argv[]) {
printf("PID OF exec1.c = %d\n", getpid());//process id

char *args[] ={ "hello", "from" , "parent", NULL};// arguments for the execv() call

//replace the current process with the exec2

execv("./exec2", args);


//this line will not be reached if execv() is successful
printf("back to exec1");
return 0;



}
Binary file added day2/exec2
Binary file not shown.
11 changes: 11 additions & 0 deletions day2/exec2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include<stdio.h>
#include<unistd.h>//process id
#include<stdlib.h>

int main (int argc, char *argv[]){
printf("hello from exec2\n");
printf("PID of the exec2 is :%d\n", getpid());//process id
return 0;


}
Binary file added day2/fork
Binary file not shown.
30 changes: 30 additions & 0 deletions day2/fork.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<stdio.h>//input and output
#include<stdlib.h>
#include<unistd.h>//getpid()
#include<sys/types.h>

int main(int argc, char *argv){
//fork to create a new process
pid_t pid = fork();

//check if fork was successful
if (pid == 0 ){
//code for the child
printf("this is the child process and the pid is:%d\n", getpid());//shows the simultaneous run ofchild and parets
exit(0);// terminate the child process
}

else if (pid >0){
printf("this is the parent process and pid is :%d\n",getpid());
//parent
}

//error handling
else{
printf("error while forking\n");
exit(EXIT_FAILURE);
}
return 0;


}
48 changes: 48 additions & 0 deletions day3/alarm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<iostream>
#include<sys/types.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>

static int alarm_fired = 0;

void ding(int sig){
alarm_fired = 1;
}

int main(){
pid_t pid;
std::cout<<"alarm starting!\n";//not using namespacestd thats why std
pid = fork();

switch(pid){
case -1:
{
//error handling
perror("fork failed\n");
exit(1);
break;
}
case 0:
{
//child block
sleep(5);
pid_t parentpid = getpid();

kill(parentpid, SIGUSR1);
exit(0);
break;
}
default:
{
//parent
std::cout<<"waiting for alarm to go off\n";
(void) signal(SIGUSR1, ding);
pause();
if(alarm_fired)std::cout<<"alarm fired\n";
std::cout<<"done\n";

}
}
exit(0);
}
31 changes: 31 additions & 0 deletions day3/wait.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<stdio.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<unistd.h>

int main(){
//create a child
pid_t child = fork();//fork returns 0
int exitStatus , childPid;

if(child == 0){
//display childnrunning
//put the child to sleep
printf("child: running and pid:%d\n", getpid());

sleep(5);

//exit the child
exit(40);
}else if(child > 0){
//parent
printf("parent: running with pid:%d\n", getpid());
childPid = wait(&exitStatus);
printf("parent: child finished it had the pid: %d, exitstatus:%d\n", getpid(), WEXITSTATUS(exitStatus));

}else{
printf("cant fork\n");
exit(1);
}
return 0;
}
Binary file added day5/container
Binary file not shown.
38 changes: 38 additions & 0 deletions day5/container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>

using namespace std;

#define CGROUP_SUBTREE "/sys/fs/cgroup/cgroup.subtree_control"
#define REQ_CGROUP "/sys/fs/cgroup/cntr"

void WRITE(const char* path, const char* value) {
int fd = open(path, O_WRONLY | O_APPEND);
if (fd == -1) {
perror("Error in opening");
exit(1);
}

ssize_t bytes = write(fd, value, strlen(value));
if (bytes == -1) {
perror("Error in writing");
}

close(fd);
}

void makeCgroup() {
WRITE(CGROUP_SUBTREE, "+cpu +memory +pids"); // Corrected subsystem names
mkdir(REQ_CGROUP, S_IRUSR | S_IWUSR | S_IXUSR); // Added execute permission
}

int main() {
cout << "parent pid: " << getpid() << endl;
makeCgroup();

return 0;
}
95 changes: 95 additions & 0 deletions day7/container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "parser.h"
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/wait.h>
#define concat(a,b) (a"" b)
#define CGROUP_MEM "/sys/fs/cgroup/cntr/memory.max"
#define CGROUP_CPU "/sys/fs/cgroup/cntr/cpu.max"
#define CGROUP_SUBTREE "/sys/fs/cgroup/cgroup.subtree_control"
#define REQ_CGROUP "/sys/fs/cgroup/cntr"
using namespace std;


int isOK(int status,const char* msg){
if(status==-1){
perror(msg);
exit(EXIT_FAILURE);
}
return status;
}

void WRITE(const char* path,const char* value){
int fd = open(path,O_WRONLY | O_APPEND);
if(fd == -1){
cout<<"Error in opening\n";
exit(1);
}
ssize_t bytes = write(fd,value,strlen(value));
if(bytes==-1){
cout<<"error in writing\n";
exit(1);
}
close(fd);

}
char* stack_mem(){
const int stacksize = 65*1024;
auto *stack = new (nothrow) char[stacksize];
if(stack==nullptr){
cout<<"Can't allocate memory";
exit(EXIT_FAILURE);
}
return stack+stacksize;
}
void cloneProcess(int (*function)(void*),int flags){
auto pid= clone(function,stack_mem(),flags,0);
isOK(pid,"Clone Process Error");
wait(nullptr);

}
void setupENV(){
clearenv();
setenv("TERM","xterm-256color",0);
setenv("PATH","/bin/:/sbin/:/usr/sbin",0);
}
void setupChild(){
map<string,string> c=parse("container_config.ini");
const char* root = c["custom_root"].c_str();
const char* cpuManage = c["cpu_manage"].c_str();
const char* memory = c["memory"].c_str();
const char* maxProcess = c["maxProcess"].c_str();
const char* hostname = c["host_name"].c_str();
WRITE(CGROUP_MEM,memory);
WRITE(CGROUP_CPU,cpuManage);
WRITE(concat(REQ_CGROUP,"/pid.max"),maxProcess);

isOK(sethostname(hostname,strlen(hostname)),"hostname error");
setupENV();






}
int childProcess(void *args){
setupChild();
return 0;
}
void makeCgroup(){
WRITE(CGROUP_SUBTREE,"+cpu +memory +pids");
mkdir(REQ_CGROUP,S_IRUSR | S_IWUSR);
const char* pid = to_string(getpid()).c_str();
// cout<<pid;
WRITE(concat(REQ_CGROUP,"/cgroup.procs"),pid);

}
int main(){
cout<<"Parent Pid :"<<getpid()<<endl;
makeCgroup();
cloneProcess();

}
2 changes: 1 addition & 1 deletion container_config.ini → day7/container_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ custom_root = ./root
maxProcess = 7
cpu_manage = 20000 100000
memory = 2G
host_name = container
host_name = container
File renamed without changes.
Binary file added day8/container
Binary file not shown.
Loading