USP Lab Programs with Video Explaination

1) To Write a C/C++ POSIX compliant program to check the following limits:

(i) No. of clock ticks
(ii) Max. no. of child processes
(iii) Max. path length
(iv) Max. no. of characters in a file name
(v) Max. no. of open files/ process

Program:

#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include "iostream"
#include <unistd.h>
using namespace std;
int main()
{
cout<<"No of clock ticks:"<<sysconf(_SC_CLK_TCK)<<endl;
cout<<"Maximum no of child processes:"<<sysconf(_SC_CHILD_MAX)<<endl;
cout<<"Maximum path length:"<<pathconf("/",_PC_PATH_MAX)<<endl;
cout<<"Maximum characters in a file name:"<<pathconf("/",_PC_NAME_MAX)<<endl;
cout<<"Maximum no of open files:"<<sysconf(_SC_OPEN_MAX)<<endl;
return 0;
}

Video Explanation of pgm1:

_ _____________________________________________________________

2)  Write a C/C++ POSIX compliant program that prints the POSIX defined configuration  options supported on any given system using feature test macros.

Test macros: are macros that are already been defined, these macros can be used to test certain  features on the host  machines

if its supported it returns boolean True value else false ,

  199309L              : Indicates Posix1V
_POSIX_SOURCE :  Indicates posix1
ifdef : is a preprocessor directory

_POSIX_JOB_CONTROL : defines in <unistd>
                                          job control features are essentially job control features, or some thing related to job control, like process waiting, process stats etc,

_POSIX_SAVED_IDS        : System supports saved setUID and saved setGID's
UID and GID can be saved in the system, In many Operating system like TINY OS these suppor tis not supported , tiny OS by very nature will load completely into the RAM and it vanished once restarted ,



_POSIX_CHOWN_RESTRICTED :If your system changing ownership of file , this particular feature is supported in your system it displays feature is supported ,

 _POSIX_NO_TRUNC: system supports path truncation option , if the file name is longer than NAME_MAX it silently truncates ,




#ifdef _POSIX_VDISABLE : if system support disable character for file , disable character means if u are able to give certain characters for the file name , these options are meaningful only to those file which are terminal devices

Program 2:

#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include "iostream"
#include<unistd.h>
using namespace std;
int main()
{
#ifdef _POSIX_JOB_CONTROL
cout<<"System supports POSIX job control:"<<_POSIX_JOB_CONTROL<<endl;
#else
cout<<"System does not support POSIX job control"<<endl;
#endif
#ifdef _POSIX_SAVED_IDS
cout<<"System supports saved set UID and GID:"<<_POSIX_SAVED_IDS<<endl;
#else
cout<<"System does not support saved set GID and UID"<<endl;
#endif
#ifdef _POSIX_CHOWN_RESTRICTED
cout<<"Chown restricted option is :"<<_POSIX_CHOWN_RESTRICTED<<endl;
#else
cout<<"Chown Restricted not defined"<<endl;
#endif
#ifdef _POSIX_NO_TRUNC
cout<<"Truncation option is :"<<_POSIX_NO_TRUNC<<endl;
#else
cout<<"Truncation Option not defined"<<endl;
#endif
#ifdef _POSIX_VDISABLE
cout<<"disable char for terminal files"<<_POSIX_VDISABLE<<endl;
#else
cout<<"char for terminal device files will not be diasbled"<<endl;
#endif
return 0;
}

Video Explanation :

___________________________________________________________ 


3) Consider the last 100 bytes as a region. Write a C/C++ program to check whether the region is locked or not. If the region is locked, print pid of the process which has locked. Ifthe region is not locked, lock the region with an exclusive lock, read the last 50 bytes and unlock the region.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
int main(int argc, char* argv[])
{


// structure of flock here paramater 3 : 0 represent offset, next 100 represent size of the file to lock and last parameter 0 is PID, when PID =0 it means no process has  acquired any lock on the file 



struct flock fl={F_UNLCK,SEEK_SET, 0,100,0};
int fd;
int fsize,offset;
char buf[50];

//open the file from the command line argument

if(fd=open(argv[1],O_RDWR)==-1)
{
printf(" cant open the file /n");
exit(1);
}

printf(" file is not locked by any process /n");
printf(" Press enter the Lock the file /n");
printf("***************************************************8 /n");
getchar();



// after you press enter u are replacing the l_type from unlock to type write lock and assigning the process id by  system call getpid()

 
fl.l_type=F_WRLCK;
fl.l_pid=getpid();

if(fcntl(fd,F_SETLK,&fl)==-1)
{
printf(" Cant set Exclusive Lock for the file /n");
exit(1);
}

else if(fl.l_type!=F_UNLCK)
{
printf(" the file is locked by the process ID : %d", fl.l_pid);
}

printf(" Press enter to release the lock /n");
getchar();
fl.l_type=F_UNLCK;
printf(" File is unlocked");

// now to acceess the last 100 bytes of the file

fsize=lseek(fd,0,SEEK_END);
offset=fsize-50;
lseek(fd,offset,SEEK_SET);
read(fd,buf,50);
printf(" the last 50 byets of the data are %s", buf);
return 0;
}



Theory :
flock is a structure in fcntl try man fcntl , you will notice it has 5 paramaters
        

   struct flock {
               ...
               short l_type;    /* Type of lock: F_RDLCK,
                                   F_WRLCK, F_UNLCK */
               short l_whence;  /* How to interpret l_start:
                                   SEEK_SET, SEEK_CUR, SEEK_END */
               off_t l_start;   /* Starting offset for lock */
               off_t l_len;     /* Number of bytes to lock */
               pid_t l_pid;     /* PID of process blocking our lock
                                   (F_GETLK only) */
               ...
           };


rest explanation find out in comment line in the program


Video Expl:


____ ___________________________________________________________________

4)


Coming soon  .....................................











Previous
Next Post »

1 comments:

Click here for comments

Please Leave ur Valuable comments Here ConversionConversion EmoticonEmoticon

Facebook

HTML Comment Box is loading comments...

Our Partners

Our Partners