{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "084ed6b6-cee6-4e73-928a-c88aae1a6bb2",
   "metadata": {},
   "source": [
    "<h1>Lab I: Tasks</h1>\n",
    "<hr />\n",
    "<h3>Task 1: <ccode>ps</ccode> command basics</h3>\n",
    "<spt><b>DO NOT REMOVE OR MODIFY THE FIRST LINE BEGINNING WITH %%\n",
    "</b></spt>. You can run the commands by typing &lt;SHIFT&gt; and &lt;ENTER&gt; together with the cursor in the cell. You will see that it runs the command and gives you some output. If it doesn't, please contact your nearest TA."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afaea7d1-09af-4148-bb09-5a663feae7b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "# Run the command below by typing SHIFT+ENTER in this cell\n",
    "ps | nl"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66ae9c08-32ff-4a24-a050-4074600ba385",
   "metadata": {},
   "source": [
    "Answer the following questions based on the output above by typing them in the blank line given after each question. You can double-click here to see the HTML text into which you can type your answer.\n",
    "<ol>\n",
    "    <li>How many processes are on the system?\n",
    "        <p>Ans: </p></li>\n",
    "    <li>What do you think are valid values for PID?\n",
    "        <p>Ans: </p></li>\n",
    "</ol>\n",
    "You can either search the Internet or type <tt>man ps</tt> in your second terminal."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "516a9855-90b7-44da-9295-29f8c6ffa609",
   "metadata": {},
   "source": [
    "<hr />\n",
    "<h3>Task 2: Looking at <b>ALL THE PROCESSES</b> in the system</h3>\n",
    "Run the code in the next cell to see many more processes than before. You will have to be quite good with your mouse control to navigate the output produced by the command in the next cell :-) It is a very important output. We will have several questions about it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0577dedb-da46-4536-aa86-d2a35a958637",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "ps ax | nl"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4660483a-db08-4e71-ba3a-b87cab19ddb1",
   "metadata": {},
   "source": [
    "Answer the following questions (same procedure as for Task 1) based on the output above.\n",
    "<ol>\n",
    "    <li>Talk to a few of your friends and find out if the <b>process with PID = 1</b> is\n",
    "    the same for all of you. If so, which process is it? If not, list at least three\n",
    "    different processes which have PID = 1.\n",
    "    <p>Ans: </p></li>\n",
    "    <li>Why are some process names enclosed in square brackets?\n",
    "    <p>Ans: </p></li>\n",
    "    <li>Are there any values other than ? in the TTY column? What do these mean?\n",
    "    <p>Ans: </p></li>\n",
    "    <li>How many process states are there in Linux? Search the Internet for this.\n",
    "    <p>Ans: </p></li>\n",
    "    <li>How many process states did you see in the above output?\n",
    "    <p>Ans: </p></li>\n",
    "    <li>How many processes are in <b>R</b> state? If it is &gt;1, explain.\n",
    "    <p>Ans: </p></li>\n",
    "    <li>Some states are given by multicharacter names. What do the extra <b>s</b>\n",
    "    and <b>l</b> mean?\n",
    "    <p>Ans: </p></li>\n",
    "</ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a864ab1d-d928-4b32-af69-9d1b4e4cc854",
   "metadata": {},
   "source": [
    "<hr />\n",
    "<h3>Task 3: Lots more information!</h3>\n",
    "Run the command in the next cell: note the <tt>l</tt> option. It gives a <b>long</b> listing. Our job is to explore what all the extra information means. By now, you should know how to run commands in a cell!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8c9223a-c470-4f4a-9c6f-c07697f837fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "ps l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6195c106-1569-4ccc-9cd7-823090451da2",
   "metadata": {},
   "source": [
    "Answer the following questions &mdash; you know the routine :-)\n",
    "<ol>\n",
    "    <li>Some processes have a value of 4 in the first columns while others have 0. \n",
    "    What does the <b>F</b> column give and what do the values mean?\n",
    "    <p>Ans: </p></li>\n",
    "    <li>Pick any one process of your choice in the output above. How much memory is it\n",
    "    using? Type <tt>man ps</tt> in your second terminal to find out what each column in\n",
    "        the output means.\n",
    "    <p>Ans: </p></li>\n",
    "    <li>What are <b>RSS</b> and <b>VSZ</b> values?\n",
    "    <p>Ans: </p></li>\n",
    "</ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2dd6029-31b4-4335-8eaf-c2484ed61d2d",
   "metadata": {},
   "source": [
    "<hr />\n",
    "<h3>Task 4: Tracing a Process Tree</h3>\n",
    "Run the <tt>ps</tt> command with <tt>lx</tt> option in the cell below. This is tough, now ... there are so many more processes. Find the <b>last process listed</b> and from its PID and PPID, trace its ancestors as far back as you can. Give its complete list of ancestors, their PIDs and names.\n",
    "<p>\n",
    "    Ans:\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0df3a6c-7395-4b91-864a-78383516da73",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcb8920f-6bb1-4f59-b18a-1a6a4c6bc2c7",
   "metadata": {},
   "source": [
    "<hr />\n",
    "<h3>Task 5: Onward to <tt>fork()</tt></h3>\n",
    "We are switching gears here and moving from knowing to <em>creating</em> processes. Examine how <tt>fork()</tt> copies everything ... and we mean everything!\n",
    "<p>\n",
    "    Look at the C program in the cell below. Again, <b>DO NOT MODIFY OR REMOVE THE FIRST \n",
    "        LINE</b>. There is an <tt>extern</tt> and an <tt>auto</tt> variable. First, try \n",
    "    to guess what its output would be. No cheating, do not run the program.\n",
    "</p>\n",
    "Write your output here.\n",
    "<p>\n",
    "    Output:\n",
    "</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "554c926a-0d48-4b89-a925-9f83cd76dd25",
   "metadata": {},
   "source": [
    "Now run the program in the cell below. It will save your C program to the filename given in the first line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3597c98-cc30-41e3-9d7d-fc964ca767bb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%file lab01_fork.c\n",
    "#include <stdio.h>\n",
    "#include <stdlib.h>\n",
    "#include <unistd.h>\n",
    "#include <string.h>\n",
    "#include <malloc.h>\n",
    "char *h;\n",
    "int main(void)\n",
    "{\n",
    "    int pid, x = 5;                        /* Technically should be pid_t pid */\n",
    "\n",
    "    h = malloc(10);\n",
    "    strcpy(h, \"Hello!\");\n",
    "    if ((pid = fork()) != 0) {\n",
    "        sleep(1 + (int)(rand()) % 3);\n",
    "        strcpy(h, \"World!\");\n",
    "        printf(\"I am the parent! Child has pid: %d\\n\", pid);\n",
    "        x = x + 10;\n",
    "    } else {\n",
    "        sleep(1 + (int)(rand()) % 3);\n",
    "        printf(\"I am the child. my PID is %d\\n\", getpid());\n",
    "        x = x * 5;\n",
    "    }\n",
    "\n",
    "    printf(\"h = %s\\nx = %d\\n\", h, x);\n",
    "    exit(0);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73b9aa20-bc6d-41d1-8020-55a8bdb945a2",
   "metadata": {},
   "source": [
    "Run the cell below to compile the program and create an executable called <tt>lab01_fork</tt>. You should not get any errors, but if you do, call your nearest TA."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31dcf198-2eeb-4b96-a0ce-27c4fa70940e",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "gcc lab01_fork.c -o lab01_fork"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30633286-3be1-4047-a132-42cd1ff39984",
   "metadata": {},
   "source": [
    "Run the program by running the command in the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b688a65-1da3-45ab-8a81-47442d6f1866",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "./lab01_fork"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea14613a-8443-48c7-aee4-294a8adb5819",
   "metadata": {},
   "source": [
    "Did the output match what you guessed? Explain how exactly the program produced the above output.\n",
    "<h4>Explanation:</h4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71238a96-5246-4020-bfb1-3658e90644cb",
   "metadata": {},
   "source": [
    "<hr />\n",
    "<h3>Task 6: Exploring signal behaviour</h3>\n",
    "Now that we saw <tt>fork()</tt>, how can we leave our beloved <tt>exec()</tt>? Let us do the same as in Task 5. Start by looking at the code in the cell below. Predict its output and write it here.\n",
    "<p>\n",
    "    Output:\n",
    "</p>\n",
    "<p>When you run the cell below, it will save the code into a file with name given in the first line.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dcbdca10-987b-41ab-8e04-0b54529bf78d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file lab01_segv.c\n",
    "#include <stdio.h>\n",
    "#include <stdlib.h>\n",
    "#include <signal.h>\n",
    "#include <unistd.h>\n",
    "#include <string.h>\n",
    "\n",
    "void taunt_user(int);\n",
    "\n",
    "int main(void)\n",
    "{\n",
    "     int pid;\n",
    "     char *nullptr = NULL;\n",
    "     struct sigaction newact;\n",
    "     \n",
    "     newact.sa_handler = taunt_user;\n",
    "     sigfillset(&newact.sa_mask);\n",
    "//     sigdelset(&newact.sa_mask, SIGSEGV);\n",
    "//     sigdelset(&newact.sa_mask, SIGINT);\n",
    "     sigprocmask(SIG_BLOCK, &newact.sa_mask, NULL);\n",
    "\n",
    "     if (sigaction(SIGINT, &newact, NULL) < 0) {\n",
    "       fprintf(stderr, \"Error in sinal actions!\\n\");\n",
    "       exit(1);\n",
    "     }\n",
    "     if (sigaction(SIGSEGV, &newact, NULL) < 0) {\n",
    "          fprintf(stderr, \"Error in sinal actions!\\n\");\n",
    "          exit(1);\n",
    "     }\n",
    "     printf(\"PID: %d\\n\", getpid());\n",
    "     pause();\n",
    "     \n",
    "     strcpy(nullptr, \"Hello\");  /* Guaranteed to generate a segmentation violation! */\n",
    "     \n",
    "     exit(0);\n",
    "}\n",
    "\n",
    "void taunt_user(int signum)\n",
    "{\n",
    "     printf(\"Received Signal No. %d\\n\", signum);\n",
    "     printf(\"^C does not do anything to me ... bet you cannot kill me!\\n\");\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa8b9dad-a164-4a79-a11a-c50c1c220831",
   "metadata": {},
   "source": [
    "Compile the code by running the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06a0c754-ca8a-4326-9689-a65ee8e00aeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "gcc lab01_segv.c -o lab01_segv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0654b1c2-d3cc-4279-932a-f55226bc8833",
   "metadata": {},
   "source": [
    "<b>Run the executable, <tt>lab01_segv</tt> in your second terminal.</b> It will print the\n",
    "PID and block. Make a note of the PID (believe me, you will need it!). Open another terminal so that you now have three terminals. \n",
    "<dl>\n",
    "    <dt>Expt 1:</dt>\n",
    "    <dd>Send a few of your favourite signals to the process from your third terminal.\n",
    "        What happened? Explain in the cell below. Kill the process by sending the\n",
    "        <tt>KILL</tt> signal if you haven't already done that.</dd>\n",
    "    <dt>Expt 2:</dt>\n",
    "    <dd><b>Uncomment the lines 18, 19 in the code above.</b> Now run the program\n",
    "        again in your second terminal. Make a note of the PID. Type CTRL-C \n",
    "        and see what happens. Go to the third terminal and kill the process.\n",
    "        Explain what happened in the cell below.</dd>\n",
    "</dl>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db01c55e-938b-4ddc-a869-9c4de16337b5",
   "metadata": {},
   "source": [
    "<h4>SIGSEGV program explanation</h4>\n",
    "<b>Expt 1</b>\n",
    "<p>\n",
    "    \n",
    "</p>\n",
    "<b>Expt 2</b>\n",
    "<p>\n",
    "    \n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ddc6262-691f-4357-970a-5975ca51996b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5a7f6ae6-6715-43a8-b758-7ca0ee17812e",
   "metadata": {},
   "source": [
    "<p>\n",
    "    <hr>\n",
    "    <center>THE END</center>\n",
    "    <hr>\n",
    "</p>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
