In 2001, Danish political scientist Bjorn Lomborg burst onto the international scene with his best-selling and controversial book The Skeptical Environmentalist. The one-time member of Greenpeace said that climate change is real and that human activity is clearly contributing to it, but he said the best signs didn't support the apocalyptic visions put forth by people like Earth and the Balance Author and former Vice President Al Gore. Lomborg went on to create the Copenhagen consensus, a think tank that applies cost-benefit analysis to problems facing the global poor and works with Nobel laureates, policymakers, philanthropists and researchers to develop pragmatic, relatively low-cost solutions to issues such as tuberculosis, malaria, lack of education, and lack of access to food. His new book is called Best Things First, and it presents what Lomborg says are the 12 most efficient solutions for the world's poorest people. He argues that for about $35 billion a year, a little more than half of what the U.S. spends annually on humanitarian aid, these policies would save 4.2 million lives and generate an extra $1.1 trillion in value every year. Reason caught up with Lomborg and New York City during the latest meeting of the United Nations General Assembly. We talked about Best Things First and why politicians and the media continue to fixate on the possibility of future climate apocalypse rather than helping the global poor in the here and now. Bjorn Lomborg, thanks for talking to Riz. It's great to be here. Thank you. Let's start off by talking about your new book, Best Things First, which is based on the Copenhagen consensus project that you've developed over the past couple of decades. First, what is the Copenhagen consensus? So we're really a very small think tank that worked with hundreds of the world's top economists to ask a very simple question, how do you get the biggest bang for your buck? Look, there are lots of stuff you can do. Not all of it is equally good. So we simply try to sort of push for the really smart things you can do for whatever it is that you want to achieve. Do you want to get better health care? Or do you want to do something about global warming? Or do you want to do good for the world? The latter thing is what this book is about. It basically asks a very simple question, if you're going to spend an extra dollar or preferably a couple hundred million dollars or something, where can you get a lot of bang for your buck? Right. So the subtitle is the 12 most efficient solutions for the world's poorest and our global sustainable development goals promises. First, what are the sustainable development goals that were hashed out in 2015 by the United Nations? Because they were essentially the UN's attempt to try to say, what should we do for the world? What should we promise for the next 15 years from 2016 to 2030? So in 2023, we're now halfway, sorry, we're at half time for these promises, but nowhere near halfway. So we started out saying, what should we promise? And obviously you want to reduce poverty. Or actually they went so far to say, get rid of poverty. We should also reduce hunger or actually get rid of hunger. Oh, we should also fix war. We should fix corruption. We should fix climate change. We should fix education. It ended up being essentially an almost endless list of promises to do all good things in the world and make sure that people get organic apples and know about indigenous people and have community gardens and the whole shebang. Yeah, it's a very beautiful list of all the stuff that you'd ever want if money was no object, but it is. And UN Secretary General Antonin Guterres, he's calling for a $500 billion kind of global stimulus project. You think that's generally wasteful and unrealistic. He's focusing mostly on renewable energy, universal social protection, and job creation. Why is that misguided? So first of all, you're not going to get people to give half a trillion dollars more to a project that's failing. So if you have a project where you promised everything, then after seven and a half years ago, wow, we didn't make it. Give us more money. That doesn't really sound very reassuring. But also what you have to realize is if you're actually going to deliver on all of the things you promised, half a trillion dollars is not going to cut it. You will need somewhere between 10 and 15 trillion dollars to deliver all the things we promised. And of course, that's the same amount of money that all the governments in the world are right now raising in taxes. Essentially, the argument is if we want it to deliver on everything we promised back in 2015, we'd have to double all global taxes. Newsflash not going to happen. Nobody will want that. And so you really have to go back and say, instead of spending lots and lots of extra money, which I just don't see politically as feasible. Also, on the particular things that he's talking about, you know, more renewable energy, nice. If you're rich, you can actually afford it. It feels good. It makes you feel like you're all green and virtuous, but it's by no means the most effective thing you can do for the world. And so what we're saying is, look, you might be able to get a little more money. Let's make sure we focus that little more money on the very best things you can do. Yeah. So you're kind of the calculations that you make in the book and it's cost-benefit analysis, which you confusingly call benefit cost solutions or ratios and things like that. But is that for about $35 billion a year, you say we could say 4.2 million lives a year and generate $1.1 trillion in extra value a year. And to put that in perspective, the US currently spends $60 billion annually in humanitarian aid, philanthropists I guess in the US kick in about another $30 billion. So for a little bit more than half of what the US government is already spending, you're saying here are 12 things that could really radically alter it. Let's talk about some of the 12 solutions that you come up with. At the top of the list is basically ending tuberculosis. How does that happen and why is it so cheap to end tuberculosis? I should just say it's not on the top of the list. It's just the first thing on the list. It's my 12 children and I'm not going to pick one of them. Well, it's all Sophie's choices. They're all incredibly good choices. So one of them is tuberculosis. And it's this astounding thing that, remember, tuberculosis brought on almost a tsunami of death over the last couple of hundred years. If you look back in the 1800s, every fourth person that died in the US and in Europe died from tuberculosis. We estimate over the last 200 years about a billion people died from tuberculosis. And then we fixed it. We got antibiotics. And that basically fixed it in the rich world. Nobody dies for a very, very few people died from tuberculosis anymore. But that's not so in the poor part of the world. Every year, it still kills 1.4 million people. That makes it the most deadly infectious disease for the last decade every year except the two years with COVID. So how do you address that? So again, it's very simple. You simply need to get a pill. Well, actually, you need to get a pill every day for six months. And this is one of the reasons why it's hard. You actually need to keep taking it way after you got better. And if you've ever tried to take pills for 14 days, once you get fine, it's sort of hard to remember. So it's actually, you have to stimulate people. You have to make apps. You may have to make sort of efforts. Maybe you have tuberculosis anonymous where we meet every week and say, yes, I took my pills. That kind of thing. Maybe give out juice cartons as premiums for making sure people do it. It sounds a little silly, but remember, every person that you can cure from tuberculosis is a person that won't put on tuberculosis for another 10 or 15 people for one year. So it's a very good idea. The second part of it is to test a lot of people. So a lot of people don't know that they have tuberculosis, partly because they don't want to know. It has a lot of taboo in Kenya, for instance, if you get diagnosed with tuberculosis, one in four people will lose their spouse because they'll divorce them because, you know, who wants that? And also you'll have problems at work. Maybe you'll lose your job, all these kinds of things. So what you have to do is to make sure that people recognize this is actually very treatable disease. We've discovered it. You're getting treatment. You're fine. This will cost more money, but it'll also basically save about a million people each and every year. So for about $6 billion, we can save a million people each and every year. And that it's related to another disease that's on the list, malaria. And it's funny when you talk about tuberculosis being a massive killer in Europe and America really until the last century, malaria. I mean, it's amazing to think that Washington, DC once and Philadelphia had massive malaria problems in American history. How do you fix malaria? We think malaria is a tropical disease, but really that's only true because that's the only place where we haven't gotten rid of it. Remember, 36 states in the US had endemic malaria. Malaria was endemic in Moscow and in Finland. It's not a tropical disease. It's a disease of poverty.  And of course, once you get rich enough, you get treatment and then you get rid of malaria. This has not happened in sub-Saharan Africa. That's almost entirely where the problem is now. So what, and there's a number of reasons for it. They have the most deadly variant of malaria. They have a mosquito that doesn't want to bite the livestock, so they only bite people. And so it's much harder to get rid of. We know how to do it. It's simply about getting many more people to sleep under bed nets. And the nets you write are, the best nets are treated with insecticide. Is that DDC as well? No, no, that's no longer what you use, but it is a very cheap pesticide. Unfortunately, the mosquitoes are now starting to become resistant to that. So they've moved to slightly more expensive. We've made our calculations involve that you both use some of the cheap ones and some of the more expensive ones, depending on where you do it. And that's what is reflected in the calculations. You talk about education globally, and there's a stat in there which up now, I thought I had written that it was something like 80% of young people around the world can't do basic math or reading. So education seems to be one of those things where everybody, and you write about this, everybody is constantly fixing it. Virtually every country is spending more on education than they were 20, 30, 40 years ago, and the results aren't better. So we went through the one laptop per child fad which didn't do anything. How do you address global problems with education? Education is a great example of what we try to do in general, namely that everybody agrees in principle that we should do education, but then they mean a thousand different things with it. But the reality is, the very best way to deal with education is to focus on primary education because that's where the efforts are cheapest and the impacts are the greatest. So it's not about getting, you know, tertiary and university education, it's about getting primary education. As you mentioned, 80% of 10-year-old kids in low and low and middle income countries are functionally illiterate. Now, they can read, they can read the individual words, but if you ask them, and this is one of those questions where you ask them, read this sentence that says, VJ has a red hat, blue shirt, and yellow shoes. What color is the hat? It's not hard, it is red, right? But unfortunately, they can read the individual words, but they don't string it together into a sentence. So 80% fail on this very, very basic thing. And of course, what that means is you're not gonna be very productive when you become an adult. We need to teach these kids better. The problem with all education, this is also true in rich countries, but especially in poor countries, is that you have perhaps 50 kids, they're all 12-year-olds in the same grade, but they're vastly different in ability. Some of these kids are far beyond what the teacher is teaching. Some of these kids have virtually no clue what's going on. So they are at the board or almost lost in there. And what the teacher should do is teach each one of these kids at his or her own level. But of course, you can't do that if you have 50, but technology can. So if you put each one of these kids in front of a tablet with educational software, just for one hour a day, this educational software will very quickly pick up on where exactly this girl or boy is at and say, oh, okay, I'm gonna teach you from here on. And what happens is this educational software can then teach you so well that even though you still go to school for seven hours that are still very boring and you learn very little, if you just do this one hour a day, the benefit of course is that you can then share the tablet with many others so it becomes cheaper. One hour a day for a year, you will learn as much as you normally would have learned for three years. So for $31 for this thing. And again, this is very, very well tested. It assumes that some of this money is gonna go to corruption. Some of it is gonna be misused because people are incompetent. Some of these tablets are gonna get stolen. You'll also need to have solar panels to recharge them and all this stuff. All of this was included in the cost and we know these things work. For $31, you can teach a kid three years of schooling instead of one year. And that means that kid will become better and more productive in its adult life. And that translates into about $2,000 in higher incomes. If we did this for all the almost 600 million kids, that would generate an immense amount of opportunity which is about $600 billion, a very large part of the $1.1 trillion you mentioned before. So fundamentally, we can fix a very simple problem. It is not saying fix all problems in education. Just like we're not saying fix all problems in all kinds of other ways. We're saying here are 12 very specific things that actually are well documented to work incredibly well at very low cost. Let's do those first. I seem to remember in earlier iterations of the Copenhagen consensus findings that there was an emphasis on education for women or for girls. Has that been folded into all of this or what happened to that focus? Has it been broadened? So this has been brought about by us asking the economists now what is the very best documented thing that we believe would deliver a very high amount of benefits. And that was exactly as we just talked about, learning at the right level. It's actually not just that. It's also one other thing, structured teacher planning. So basically teach the teachers to be better teachers because often these teachers are just barely better than the kids that they're teaching and they're now very good. Help them give them structured teacher plans and they'll also make them better. There's also a whole focus on getting girls and girls better educated. But remember that was very much a focus 10, 20 years ago when girls were far behind. They just didn't get into school. So a very large focus in past was on simply getting kids in school. That makes sense. If you don't have kids in school you first wanna get them in school. What we're learning now is they're in school but they're just not learning anything. And so now the movement is not any longer to get kids in school and special girls into school. It's about teaching them stuff while they're in there. And that's where the tablets come in. I'm skipping around a little bit but you put a big premium on agricultural R&D. You write that we need a new green revolution. What was the green revolution and how do we make that happen in the 21st century? If you think back in the 1960s a lot of people were very, very worried that we wouldn't be able to feed the world. Paula Ehrlich was talking about India's basket case.  That certainly seemed like- Very big of him to be like, ah, that's the breaks. Yes, well, in some sense if it really was true then we probably should triage. I don't, I think he was probably right given the frame of mind back then but we were wrong because there was actually a really, really simple solution which was the green revolution. It was make every seed produce much more food. If you do that, you still have all the same farmers they plant the same makers but much more food comes up. That was what the green revolution did but it did it mostly for wheat, rice and corn. This is great for us and for people who live off of this but much especially in Africa and some places in Southeast Asia live from other things like sorghum and cassava and all these other things that we haven't even heard of but need their own green revolution. We haven't been focused nearly as much on this. So when people talk about the fact that there's still about it almost a billion people that are getting malnourished the idea is to somehow to say, well, we need to get more food to them. Well, it's really hard to and they do it in India for instance, you buy up lots of food and then you distribute it to poor people. First of all, when you buy up lots of food you're gonna get the worst food because they will sell it to governments. I met with Gandhi who was actually distributing this for Modi and she said try and eat one of these cakes. Nobody wants it, right? I mean, it's horrible. Thanks, I'll starve. Yeah, I mean, look, you really have to starve in order to buy this but also getting it distributed is really hard and cost a lot of money. It's much smarter. And that's what our economists told us. Instead of trying to distribute a lot of extra food make agriculture more efficient. So invest a lot more in R&D that's much cheaper and what will happen is you will both make the farmers more productive. That's great for them because they'll actually make more money but you'll also make prices on each of these agricultural products lower which is great for all the cities dwelling people who will buy the food. So it's both better for producers, better for consumers. And that means we get about 130 million fewer people starving. It's simply a fantastic investment.  How confident are you that, okay, we throw more money at R&D and it will figure it out. So we're very confident on that particular area because we've been doing it for a very long time and none of these assume that we're gonna find this amazing breakthrough that we're giving it to a lot of people and they never figured out and then this one guy will get a big breakthrough. That's not how that works. Everybody, you know, so our tingers can get 1.5% better yields next year and then somebody else comes along and tinker a little more and what we basically tried to do is instead of getting 1.5, if we get 1.7% growth, then we're good over the next 50 years. That's basically what we investigate. It doesn't give you as much of the feel of, oh, I did something good right now, right? That's the problem with research and development. It works in the long run but we have very good knowledge that if you invest this money well, you will get more of this. Now remember, we're not assuming that you invest it amazing that you sort of with pure foresight see the right people that you invest in but you of course also have to be careful that you don't end up just investing in a lot of people who will then buy four-wheel drives and build a new fancy building. You have to be reasonably smart but if you are and we know how to do that, yes, then you can get these kinds of benefits. One of the other recommendations is increasing skilled migration. You also talk about increasing trade and a reason audience will be interested to know that among the economists who've been a part of the Copenhagen consensus is Vernon Smith, the Nobel Prize winner who we interviewed earlier this year and whatnot, but talk about increasing skilled migration. This is something, one of the great kind of trends of the past, certainly the past, I guess you could say 100 years but certainly the past 30 or 40 years has been the relatively free flow of people from country to country which is really kind of amazing in a historical context. But it seems now that a lot of people are kind of like, hey, you know what, we need to be building more walls, not opening more doors. So how do you, what's the benefit of increased skilled migration? Yes, so we both argue, I should say, for free trade and for more skilled migration, we're basically pointing out there's a huge economic benefit but we also recognize that especially in the transfer of people, there's a significant political issue. And so I go through, sort of economists will tell you that there are a trillion dollar bills lying on the floor that if you just open the world, pretty much all the world's poor will go to rich countries and they'll become much more productive and then we'll all be much better off. There is a little hiccup in the sense that we have about 600 million people in the rich world that are in working age and we're talking about a transfer of 2.4 billion people coming into the rich world. That's not gonna go off without a hitch. That will have enormous consequences, of course, especially for the people who are already in the rich world who will see their wages often drop. Now, overall, it means that more people will become much more productive. So it is true that there are trillion dollar bills lying on the floor but it has huge political implications which is why a lot of people are against it. What we're trying to say is skilled migration has the opportunity of doing something that you can actually get people on board with. We're not saying open up orders for skilled trade. We're saying have 10% more skilled migration than what you already have. So countries that are very comfortable like Canada with migration, they will take in 10% of a rather large number. Countries that are very skeptical would take in 10% of a very low number. The benefit is if you, for instance, get doctors or STEM workers from poor countries to rich countries, they become much, much more productive. They have the education, but they can now deliver much higher productivity. That's where the big benefit comes from. And of course, what that means is that that doctor will now be part of a much richer world which means that he or she- How does that, in that scenario, how does the money or the wealth go back to the poor country or is it a brain drain? So, yes, so the main- And I'm thinking mostly of Van Gleid where all doctors will leave Van Gleid the minute that they can. Oh, absolutely. So look, the main benefit comes to the people who actually move.  So that's the big benefit, but it's reasonable to talk about what about the brain drain? Well, we estimate that net it'll also be a benefit for the transfer country. So for the country that delivers the doctor, both because they get remittances. We know that that happens all through life, but also those remittances lead to more education for the people in that country because they know that they now have that stable money flow from remittances which means that they get better educated. So you actually end up with higher education but it is not a great deal for the sending country. It is mostly a great deal for the individuals that we move. I wanna talk about this because this is the one that struck me as kind of most new to me and interesting. You talk about e-procurement or electronic electric procurement. What is that and why, how did that make the list? It is a really, really sort of surprising thing. Most people have never heard of it. But think about it, the biggest consumer of almost everything in the world is typically the state because they buy everything from post-it notes to roads, but fundamentally most of this money is an infrastructure. And this is typically incredibly corrupt. So we worked with Bangladesh for instance and they have this old English system that whenever the government needs something new built they'll put out in this obscure paper that we want this bridge and please hand in your sealed envelope with your bid in this office. But of course the reality is that you already have the local elite have already decided you're gonna get that job and so they put up goons outside the office so you literally physically can't get in with your envelope. This is how you keep having too high cost for governments and you typically also get low quality. E-procurement is very basically, there's more to it, but very basically that you just put this project on eBay. You just ask people to say, here's what we'd like, we like this bridge, then you type in, everybody can see it, now you don't have to go to this obscure paper. It becomes much harder to put these goons up. You can still do it, but it's harder to do. So what you basically avoid is a lot of corruption and you get high quality because more people will bid. You also typically get cheaper, sorry, faster. So you basically get a lot of benefits in Bangladesh. Again, we showed that this means on average that your prices will go down by 6.75%. If you look across a wide range of countries, we also did this particular in Bangladesh, but you get things somewhat cheaper. It's probably very much the corruption that you avoid, but the benefit of course means that for Bangladesh, it means they have $700 million each in every year that they can now use in things that'll actually help the population instead of going into coffers of people who are corrupt. This is great, and the finance minister loved it in Bangladesh, but of course he also knows that the people, a couple of levels below will be really, really annoyed because that's part of their income. So there is real cost to this. You need to have political push, but it actually turns out for very little money. So we estimate there's still about 70 countries in the world that haven't implemented e-progress. Most rich countries have US, absolutely, but you can do it even more. You can do it better, but mostly these 70 countries should do it. It'll cost about $76 million. So it's the only thing that's not billions of dollars. It's super, super cheap, and what it will deliver is benefits worth about $10 billion. So every dollar spent will do $135 worth of good. That's just a fantastic thing. We should just do it. It's also, it seems related to the land tenure security, at least tenuously in terms of when things become more transparent, who owns things and they have a legitimate right to it, you just lower transaction costs tremendously. Yes, and it's a question of having security and just knowing you own stuff. That has a huge difference. Both you can use as a collateral, but also just your own life. You can actually relax and say, this is mine. It has a huge difference in how countries and how effective economies are. And again, we showed that land tenure will have significant costs because you actually need to have cadastral surveys and you need to have better resolution and all these things, but the benefits will vastly outweigh the costs. You know, this is all so compelling when I've been reading the various iterations over the year. I'm like, of course, of course, of course. What about the political economy? You've mentioned, you know, kind of, there's a lot of people invested in the system as it is who are gonna fight like cats and dogs to keep any kind of reform from coming. How do you move it forward as a actual political program that has a chance of being implemented? So I think the biggest problem that we really face is that everybody wants to feel like we're doing everything. Americans do this a lot. It's a little foreign for Europeans, but come Christmas, you'll sit and write out checks to this kennel or this drug shelter or whatever it is. And the point is, you know, any economist that look at that would say, but one of these things is the best.  And the reason is because it feels like you've done 10 times as much good, right? It feels like you've been part of solving 10 problems instead of one. So the feeling, I think, is very universal. You want to do a lot of good things. That's really why we have the sustainable development goals that just basically promise everything. Instead, we should do the smartest stuff first. That is hard because it means saying no to a lot of things. I have that wonderful quote by Steve Jobs when they ask him, what are you most proud of? You know, what is it we should really focus on? He said, it's not about saying yes to stuff, it's about saying no. I'm so proud of all the things I've said no to. You have to say no to a lot of things in order to say yes to the few things that really matter. So we should do this. So your question, sorry. No, no, no, I mean, that logic makes a lot of sense. And then it's a question of who is going to be saying yes or no. Is it something that gets forced through the UN and they develop something? Or is it individual countries that say, you know what, we are going to pursue this and we're going to focus, you know, Bangladesh, we're going to focus on E-Procurement or something like that. I think I see myself as an intellectual. I put forward an argument for 12 things that would be incredibly good. I think what it really does is it makes it a little more likely in every, you know, in the USAID or in all these different development agencies around the world in negotiations between people, if you're a philanthropist, it becomes a little more likely that people are going to pick one of those 12 things. They're not going to do all 12 of them. That's fine. But they're going to pick one of those because, oh, I read this book and it actually says this is incredibly efficient. So it pushes a little bit us more to spending money where it's really efficient and a little less on all the other things. I would love, as an economist, I would love for us to just do smart stuff. But I also recognize this is a real world. So we have a saying in our organization. It's not about getting it right. It's about getting it slightly less wrong. If we can help move the world a little bit towards funding more of the 12 and a little less of all the other stuff we want. Is there any indication that's happening? I think there is a little bit, but look, it's very, very hard to tell when we've done this in individual countries. For instance, for Bangladesh, they actually did a procurement, not just because we said it, but this is part of the movement towards that. I think they did a few other things really well. In Malawi, where we work, they actually did do, and they're right now doing tablets for kids in primary school. They're trying to get this out across the entire country, partly because of the calculations that we've done. So I think we help giving good ideas, but again, the point here is that it has to be taken up by individual philanthropists and by governments. And again, remember, USAID is trying to do a thousand things, and they're also very bound by a lot of Congress people who put different restrictions on them in all kinds of ways. And likewise, philanthropies are always, they start out, we want to save the world, but they often end up in places that just look good on TV. And I just want to make sure that people focus a little bit more on the actual effectiveness. I think we help push people to be a little smarter, but no, this is not what's going to save the whole world. You know, we've talked a little bit about the sustainable development goals from 2015 that were kind of the worst version of a U.N. Thumbsucker kind of activity. In the book, you talk about the millennium development goals and how they were successful. Can you remind us of what the millennium development goals were and why were they successful in a way that the SDGs are not going to be? Yeah. So the millennium development goals were really developed by Kofi Annan and a few other guys. There were all guys in a back room in the U.N. in around 2000. So they came up with this very, very short list of things. So let's stop poverty and hunger. Let's get all kids in school, clean drinking, water and sanitation. Stop moms from dying, stop kids from dying. How's that? So we made a very, very short list. That's actually a little longer, but everybody decided to ignore it. But mostly we just had these eight smart things. And when you set up eight things and it was a new millennium and we said, we should do something. Let's do these things. Remember, not all of these things happened because the U.N. said it. Yes, we got a lot more people out of poverty, that was mostly because China got rich. But we did actually spend a lot more money on making sure that kids didn't die. For instance, through vaccinations, we know vaccinations like against measles. It's just a good idea. And back then, almost no kids were vaccinated. After that, about 80% were vaccinated. Still not enough, but much, much better. And it saved 800,000 lives. How amazing is that? And if you add all of this up, we actually went from in 1990 about 12 million kids died each year before their fifth birthday. In 2015, when the Millennium Development Goals were done, only, and I put that in inverted commas, only 6 million kids died. Now that's still 6 million too many, but because the cohorts were about the same, that's actually 6 million kids that don't die. And that's a tremendous achievement. We didn't actually meet the targets that were a little too unrealistic, but we did a lot of good.  You can't do it about everything. And certainly they were not all equally smart, but we did manage to do a lot of good because we focused with the SDGs. We said, let's do everything for everyone. And then of course, you don't have any focus. You don't get any acceleration. There's been no measurable pickup. People have not gotten more excited of anything that you've said in a little, wow, that's a lot of things we promised. So focus on a few things and say, these are the best things you can do. That's what I try to do in the book. And basically if we can get people to say, I want to be part of one of those 12 things, that's the way you make big difference. How, you know, is there also a sense that the, let me start over, in the United States and certainly among kind of classical liberals, libertarians, I think that's probably true of Europe as well. The UN is in an increasingly terrible reputation, right? I mean, it just, it never really seems to get things done, et cetera like that. Can you talk a little bit about the massive decline in global poverty, both extreme poverty and just regular poverty, you know, has really been spectacular and it was really unexpected at the level and rate. It was how much of the decline in global poverty was due to these larger issues like you were talking about, you know, a kind of free trader globalization, China, you know, starting to turn more to a market economy and how much, or you know, how, can we say, you know, the UN's adopting certain ideas or pushing certain ideas was responsible for X percentage of that? Or is it all part of the same kind of movement towards a free world? I don't think anyone really believed that the poverty had anything to do with, you know, these grand promises from the UN or from any other place. It comes from economic development. And, you know, no leader in the world is going to say, no, I don't want to do, you know, economic development. And then the UN says, well, you should, oh, okay, then I'll do it. That's not how that works. You want to have more people in jobs. You want to get your nation richer. These are things that you don't actually need to promise. What you do need to promise perhaps much more focused is to make sure that women don't die in childbirth because that's a marginalized group of, you know, if you look at hospitals, they are often run by men. They are often about old people that are deceased. They typically the same sort of demographic as the politicians, whereas, you know, women coming in to give birth, that's not so exciting. So they're underfunded, but that's one of the places where we can make sure that both women survive much more and their kids, which are all of our kids, survive much more at very low cost. So again, this is one of the places where you can actually make a difference by focusing people's attention.  What you need to focus on, but that's much more boring, is to get it right in primary education. And so you can focus people's attention more with sort of these promises, but what you have to make sure is you don't promise everything. You have to promise a few really smart things. And that's what we're trying to say. Look, the UN should have done their job better. They didn't. And I understand why, you know, when I went around and talked to a lot of the UN ambassadors individually and talked to them about why they should pick really smart stuff. And they were all like, this is really interesting. But actually, you know, the Brazilian ambassador would tell me, but you know, my job is not to get the best targets for the world. My job is to get Brazil's five points in there. And you know, the Norwegian ambassador to get Norway's four points in there. That's how we ended up with a list of everything. Let's talk about climate change and discourse related to it. You know, it's almost 25 years ago, right? You made a name for yourself. I think, you know, you really emerged on the world stage as the skeptical environmentalist, which was the title of one of your first bucks or your first big book. What made you skeptical about environmentalist policy discussions back then? So I was an old green piece or not in the sense I was out in the rubber boat, but you know, I had the poster and the backpack and the badge.  In Denmark, yeah, the same thing. And certainly from here. But yeah, fundamentally, I was very worried. I thought the world was coming off, the wheels were coming off the cart. That's a bad metaphor the way I just formulate it. I thought things were going wrong and we needed to do better. And I thought, you know, the environmentalist was telling a very important story. The world is getting worse and worse. We need to do a lot of stuff to do it. I realized that's not what the data mostly tells us. Actually, as we've become richer, we have in most important areas, both of environmentalists and non-environment fixed a lot of problems. Remember, air pollution used to be terrible in most rich countries. It was terrible in London around 1900. Since then, air pollution has come down dramatically for many reasons, partly because air pollution is an indication that you're producing inefficiently. So you can actually do better by reducing pollution. But also as you get richer, it's also just simply, you know, I'm richer, I'd like to cough a little less. So let's do more regulation and make sure that we actually get cleaner air. We have gotten incredibly clean air. We probably have cleaner air today in most rich countries that we've had for the last at least four or 500 years. This is a tremendous achievement. We have the same thing with water and many other places. Now, not everything is fine. You know, climate change, for instance, is one of those problems that are actually getting worse. It is important to say that there are still environmental problems, but it's incredibly important to recognize we have solved many more problems than we have caused new ones. We're not sort of a plague on the planet. And of course, also, if this scare story, the end is nigh is not correct. It's probably also important to not let us be scared into spending money, but actually keep asking, what is smart spending? And that's what I did on the climate and in general on the environmental front. And I think that pissed off a lot of people because a lot of environmental movement is basically predicated on the idea, I'm going to scare you, witless, and then I'm going to ask for all your money and then we're going to fix the problem. Well, that's not how you should fix problems. You should actually get a sense of how big of a problem is it? What can we do? What will cost? What are the alternatives? So certainly in developed countries, air quality is better. I think in every city in America, the air quality is better now than it was 30 years ago or 40 years ago. And for libertarians, part of that, we have to work through it, that it's the result of regulation. It's a result of laws. But one argument is that while we've taken, we've just shoved all of our pollution onto the developing world. And when you look at emerging economies like China in particular, the air quality there is getting worse and worse. Is that an accurate read or what is that missing? So there's definitely a sense in which we've pushed some pollution. But remember, for instance, you can't push transportation pollution on other countries because you need the transportation here. And what we've done is also dramatically reduced air pollution from transportation. So we really have made our world cleaner. But remember, for poor countries, typically you'll do exactly the same thing as what America and the UK did back in the early part of industrialization said, hey, if I can get rich, I'm willing to cough more. That's how it works, that you're willing to trade off if you can get better off and send your kids to college and have a lot of other benefits, you're willing to also accept more pollution, certainly in the short run. That's what they've done in poor countries and certainly Beijing and China for a long term, long time was getting worse and worse air pollution levels. Now you've actually seen this turn in many of the richest developing countries. So Chile, for instance, I've already 20 or 30 years ago starting declining levels of air pollution. China has probably just broken this level again. But in India and other places where you're much poorer, you're still seeing air pollution levels going up. Most of this is a very conscious choice of saying we'd like to get richer and part of the way we get richer is by allowing more air pollution. It's only once you get sufficiently rich, you say, now I'd like to pay for less air pollution. Is there any reason to be worried about, well, saying to the developing world, well, we're sorry, we can't allow you to do that because the total amount of pollution is going to create a hockey stick or a ratchet effect where the planet just starts dying. Is that a legitimate argument or is that something that you don't think is supported by the data? It has some merit, but it also has a lot of problems attached with it. So remember, air pollution is really a local issue or at best a regional issue. So it makes sense to say, look, what China wants of its air pollution is mostly a problem for China. They can decide that level for themselves. But the crux here is that's not true for global warming because global warming really comes from emitting CO2 and no matter where you emit CO2, it has the same global effect just over a couple of years or so. So this is really a global issue. It doesn't help that the US cuts its emissions if it just means they're transferred over to China or elsewhere. So you really have to get everybody on board. Now, remember, nobody emits CO2 because they think it's fun or they want to annoy Al Gore. They do this because it's a side effect of using power, which is one of the most advantageous things in the world. Remember a couple of generations ago, we were basically all struggling just to get through life. Most of us working on farms, being incredibly poor with our own muscles is the only thing that we could work with. Now we have all this energy, mostly fossil fuel energy that basically provides servants. You and I and most rich people have servants equivalent to somewhere between 100 and 200 people working 24-7 for you. That's the Roomba you have, the horses in your car and all these other things that provide all these amazing benefits that we have. The point here is to say, there is a real problem from climate change, but there is also a real benefit to all of these very tangible things. When you go out and tell new countries, no, I'm sorry, I know we got rich with fossil fuels and that was really cool for us, but you can't do it. You can't develop this way. You have to use solar and wind. Now, if solar and wind was so great that they're actually cheaper, which of course a lot of people will claim, then it might make sense, but then you wouldn't have to tell the people because they would just do it because it was cheaper. But of course that's not actually true. That's why we have to subsidize them here. That's why we have to push them onto poor people. Fundamentally, solar and wind is cheaper, maybe when the sun is shining and the wind is blowing, but when it's not, it's infinitely expensive. And that's why most people just have it as a little boutique add-on. In reality, most countries will only get rich if they have lots of power, cheaply available and available 24 seven. And that will typically right now, mostly be fossil fuel. So when we're telling countries, I'm sorry, you can't have that. We are in effect telling you will have to do with less growth and in some sense, you'll have to get used to be important. That's a terrible thing. From your perspective, what's the best framework then? Because if this is a climate commons, it's a global issue. What's the framework to kind of get people to coordinate in a way that doesn't give rise to the worst case scenario? Yeah. So there's two things you have to recognize in this. One is the economically effective solution is a carbon tax. Any economist would say you should have a tax that equals the disbenefit of emitting CO2.  If you get that right in the system, the system will solve itself. That's what the market mechanism does. So first of all, this has to be about how big of a problem is climate change. Because if climate change right now, a lot of people will tell you it's an existential crisis is the end of the world. Then of course, you have to do everything and the kitchen sink to fix this problem. But that's not what the UN climate panel tells us. What it tells us is it's a problem. It's not the end of the world. And that's why you can actually fix it with a carbon tax. But you have to recognize this will always ever only be a smaller part of the actual solution. You'll get a little reduction in CO2 emissions, but that's about it. The only way you're gonna solve this is through innovation. That's how we solved all other problems. You don't solve problems by telling people, I'm sorry, could you be a little colder, a little more uncomfortable, a little poorer, but you see things that are nicer for the world. That just doesn't work in general and certainly in the long run and for all countries. The way you solve this is by making sure that green energy is actually cheaper and more effective. Look at what happened with fracking in the US. You had a situation where you used a lot of coal because that was by far the cheapest way to generate electricity. Unfortunately, coal is also the biggest polluter of CO2. What came along was you basically had this new technology that made fracking, that made gas much cheaper. Gas produces about half as much CO2 as coal. So suddenly you had a situation where something that was greener was also cheaper. Everyone switched. You had a dramatic reduction in the production of coal. But now in the US, and Joe Biden has banned fracking on federal lands, environmentalists, I was just looking at this the other day, we ran an article 10 years ago, environmentalists used to be for fracking before they were against it. No, the green movement doesn't like fracking anymore. No, and look, there's some sense to this because what they say is also we need to move, we move from a high fossil fuel to a medium fossil fuel if you will, to gas, but eventually we need to move to zero emissions. And that makes sense that then, you can't just keep on doing fracking, but you also gotta be realistic. Most of the world is gonna be using more and more fossil fuels for the foreseeable future. And so what you want to do is to make sure that China goes from coal to gas. Not to say, no, no, you can't use either. And then of course just end up having China keep on using coal. So the reality here is, if you live in the real world, you should do a better rather than a poor option instead of saying, no, it all has to be pure or nothing. You said that, what we need to do is to tax carbon at the level of its disbenefit. You make that sound like that's an easy calculation. How political ultimately is that, or how subjective is that to actually price out the cost, the negative effects of carbon? That's hard. But I think the main problem here is you have to be honest that was the value of the present versus the future or what the economists know as the discount rate. If the present is very important compared to the future, climate change is just not a very big issue. If the far future is incredibly important and the present honestly not all that important to you, then climate is a big issue. That's really what drives the correct CO2 price. So not surprisingly, most people who argue for we should do a lot for climate will say, well, the future is much more important than the present. But remember, this may be true for rich well-off people, but it's certainly not true for most people in the world because honestly, they worry about the fact that their kids might die from infectious diseases tonight. And so worrying about what the temperature is in 100 years is a very, very big luxury. So can I ask this kind of goes back to our earlier conversation. How do you get the people who need to be at the table there to say, okay, here's the framework and we're actually gonna do it? I remember you writing about the Paris Accords where when those are being torn up, I guess by Donald Trump, you pointed out that even if they had succeeded, they would have had a virtually negligible effect on the temperature change. So what's the political economy of a global carbon tax? Well, fundamentally, it's not gonna happen, but also even if you managed to do this, it would only have a smaller impact on the temperature. And that's of course why I keep saying, if you actually want to do something about climate, it all comes down to innovation. So we actually did a cost-benefit analysis just like the one we talked about just for climate. So not saying, maybe you should spend your money on tuberculosis and that, but if you just care about climate, what's the smartest thing? The long run smartest thing is to invest in innovation. Just like we talked about with agriculture, the only way you're gonna solve this is by making sure that future green energy is so cheap that everybody wants to buy it. All right, we'll leave it there. Bjorn Lomborg, thanks for talking to reason. Thank you.


Welcome back to 353. So lab 2. Everyone having fun?  If you use Signal, go down to the end of the lab document and see those flags. Use those flags and that will save you a few hours by itself. Yeah. Yeah, do not mess up your file descriptors. They are hard to debug. Other useful tip, the directory is a file descriptor. So, don't close it while you're going over the directory. Bad idea. That's the major causes. Yeah, if you use a signal handler, remember at the end of the document, it says, hey, if you use a signal handler, what happens if things randomly start failing due to system calls and like just weird errors? It says flags restart. So remember when we had that lecture where we were trying to ignore signals and we had like read and then we got an error with like interrupted and we had to handle it. Well, there's a flag for the signal to prevent that from happening, like that essay restart thing. It will automatically restart any system call that gets interrupted so you don't have to deal with that. So, that's one of the main causes of errors that I've seen so far. Other than that, it's been people messing up their file descriptors. After that, you're golden. So, yeah, when you're changing your file descriptors to bit hard to debug because printf doesn't work.  So, that's fun. So, one hacky thing you can do that kind of works is just exit with no numbers. So, like exit with 42 or 43 or something like that and then you can debug that way. So, at least know where your child process made it to. But, yeah, other than that, that one's real fun to debug. All right, speaking of fun to debug, let's continue. So, something that's a bit easier, we can go back to scheduling and see, you know, another scheduling scheme or algorithm we can use called dynamic priority scheduling. May also be called feedback scheduling in the textbook. And this is a way to have priorities and let the algorithm manage the priorities for you. So, you just set an initial priority and then it goes ahead and it adjusts them on the fly. In this case, for this algorithm, it uses set time slices, measures CPU usage across the processes that are running. Idea here is that we just increase the priority of processes that don't get any time in that time slice and decrease the priority of processes that actually get to run during that time slice. So, hopefully, you know, through adjusting priorities, we'll be able to be a bit more fair and let every process run. So, for this scheme, we can just pick the lowest number as having the highest priority. So, each process just gets assigned some priority when it started. You know, we can give it names, so P subscript N. And the way the algorithm works is it just picks the lowest priority number to schedule. If that process like yields and gives up at CPU time, then we just pick the next lowest number or next highest priority. And if we have to break ties, we just break it with the rival order. And if a lower priority number becomes ready, well, we immediately switch to it. So, we have preemption and we assume while we have time units, so we can't switch between time units or within a time unit, but we can between. Yep. The formal definition of preemption is you can just take some resources away. So, in this case, preemption means while these processes are using the CPU, means I can stop you from using the CPU and take it away and manage it. So, yeah. Mostly operating systems like controlling resources, preemptible and non-preemptible. So preemptible, things I can take away. Non-preemptible are things I can't take away. So, like, if your process wants to use memory or wants to use some files or something like that, I can't just take that away willy-nilly without causing gigantic issues. So, we can, yep. So, if it yields, so that means it can just give up its CPU time. So, it can just say I'm done executing for now, run something else. Or it could be blocked, like it could be waiting on something. So, basically means it just gives up its time. So, how this works is we record how much time each process executes for in that, well, we can call it a priority interval. So, that's like the interval of time that we will use and we'll recalculate priorities at the end of the interval. And we'll have, like, CN, we can call it C subscript N. And here, timer interrupts still occur. So, timer interrupts would probably happen. Throughout the priority interval, so we can go ahead, recalculate and maybe context switch to a new process. So, at the end of the priority interval, the formula to update the priority of all the processes is, well, the new priority for process N is equal to the last priority for process N divided by two. So, it decays a little bit. So, it gets a bit lower. So, it's more likely that this process will run. And then, plus, however much time it has actually executed for. So, if it gets to execute in that time slice, then, while we add to that, so that would get a lower priority and we would get, hopefully, a different process running. And then, at the end of the, and the C is just keeping track of how long that process executed for. So, whenever we recalculate the priorities, we recalculate the value of C back to zero for all the processes. So, we get to see how long they execute for in the next one. So, that's a bit wordy. So, we can just have a quick little example. So, we can assume, for now, that all processes just have an initial priority of zero. So, we'll say we have four processes ready to execute and arriving in order x, y, a, b. And the reason that they're named slightly different are they behave differently. So, typically, with processes, there's like two main ones we care about. So, a and b are called CPU bound processes. So, they're just doing some calculation. They just want the CPU as much as you will give it to them. And then, x and y might be like IO bound processes. So, they're processes that are blocking, that are waiting for something slower. So, like, waiting for the user to input something, which is really, really, really slow, waiting for the disk to give back some information, waiting for a network, something like that. And in this case, these processes just execute for one time unit. And then, we assume that they block for five time units. So, they're unable to execute while they are blocked. So, we have a timer interrupt occurring every one time unit. And then, each time slice is 10 time units, and their priority interval is 10 as well. So, they don't always have to match, but typically, they will match. So, the priority interval is just every time we recalculate the priorities, and the time slice is, well, how long a process could actually execute for. So, we want to figure out the scheduling. Well, since everything has the same priority, we have to break our tie with arrival order. So, in this case, we would schedule process X. So, because it's one of these IO bound processes here, so it only executes for one time unit, and then, it is no longer available for five time units. Yep. So, time slice is, like, the maximum time that a process can run without getting thrown out. And priority interval is whenever we recalculate the priorities. Typically, they're the same. So, yeah, in this case, X runs for one time unit, then it can't execute again for five time units. So, the next process in arrival order is Y. It's one of these IO bound processes, so it only executes for one time unit, and then, it's blocked for five. So, after this, we would have A run until we recalculate the priorities. So, X and Y would be ready again sometime at, you know, time equals five or six or seven or whatever. But, because they're all tied and they have the same priority, well, we would just prefer to keep executing Y and, or A and not context switch because they're tied. And this, you could adjust if you wanted to, but typically, it'll just keep on executing A. And then, at time ten, we will recalculate the priorities. And, yeah, here at time six, X is ready, Y is ready, we don't switch to them yet. So, when we recalculate the priorities, well, the initial priority of all of the processes is zero, so that explains this column. And then, we just add however long they ran for in that last interval. So, in the last ten time units, X ran for one, Y ran for one, and then A ran for eight. So, our new priorities would be one, one, zero, eight, or sorry, one, one, eight, zero backwards. So, in this case, well, now we have one process that has the lowest number or highest priority over everything else. So, we would just execute that for the next ten time units. And then, we'd recalculate after that and keep on going over and over again. So, we can, yep, yeah, yeah, yeah. So, in this case, X and Y are blocking, they're waiting for something to happen, like the kernel made a request to a hard drive or something like that, and then start executing another process. So, there's only one process running on the CPU at a time. Yeah, well, which is here it's X, then it's Y, and then it's A. So, there's only one process running on the CPU at a time. So, X is unblocked here, so because it blocks for five time units, it means it could execute if we decide to give it the CPU. Yeah, yeah, block just means it needs five time units because it's waiting for like a hard drive or something like that, waiting for something that's slow. So, it can't make any progress until that thing is done. So, it stops at one, so after five time units, we say it's ready because we could execute it again if we wanted to. But the kernel just, in this case, decides not to. All right, so we can do the same thing, but now processes, A and B have a priority of six, and the others have a priority of zero. So, because of that, X and Y, well, their priority is zero, so we'd prefer to run those, and X came before Y, so we would run X first. Run X for its one time unit, it gets blocked for five, so it's not available again until time six. So, next process is Y, runs for one time unit, blocks for five, so it's not available until time seven. So, in this case, well, all the processes with priority zero are blocked, so I have to pick something else to run. So, the next highest priority is a six, which is A and B, and A came before B, so I would pick A to run. Now, when X is actually ready to get unblocked and it can actually execute again at time six, because it has a higher priority than A, it immediately just kicks it out and we contact switch to X. It runs for one time unit, and then it gets blocked again for five. And then at time seven, well, Y is ready again, so it would execute for one time unit, then it gets blocked again for five. Then, well, there's nothing else to run but A again, so A would run for the remaining two time units until we have to recalculate the priorities. And if we recalculate the priorities, well, for process X, the initial is zero, ran for two, so its new priority is two. For Y, initial was zero, ran for two, new priority is two. For A, well, its original priority was six, divide by two, that's three, and then it ran for six. So its new priority is nine, and then for process B, its new priority is just three, cuz it didn't run for any time. So now, because of this, well, at time ten, X is currently still blocked, and Y is currently still blocked. So the next highest priority is process B, so I would choose to execute process B for two more time units until X becomes unblocked. So now at time 12, X is unblocked, it has a higher priority than B. So we immediately execute X for one time unit. Again, it gets blocked for five. Then Y is ready, execute for one, gets blocked for five. And then our next highest available process would be B. So it runs for four, and then X is ready again, goes X, and then it goes Y, and it goes on and on and on like that. So questions about this? It's just one way to just let the algorithm handle the priorities for us. Not terribly exciting, right? But it's a thing you can use. Let's go on to more exciting stuff. We still like virtual memory, right? Ish, no, what the, virtual memory is great. All right, I'll show you why virtual memory is great, because you will appear to be a wizard, cuz LLMs are a thing that people like, right, they're cool, they're hip, they're trendy. So if you have like a 30 billion parameter large language model, well that model itself is like 30 gigs of RAM, like it's 30 gigs large. And if you have taken this course, you can actually optimize things a little bit. So that 30 billion parameter model can actually run using only like seven gigs of RAM, and how? So it's so efficient that people are like, they don't even believe it. So we can see the discussion if it works, plus. So yeah, I got the number on, it's only 5.8. But we can kind of read that, I don't know how this is possible.  Like how is this even possible? And we should be able to explain this because we, from taking the course, should count as someone smarter than this random person on the internet. So let's take a brief detour and see. Wait, question? So question, how does the hard coded priorities we saw in the previous lecture being incorporated with this? Is it just the initial priority, negative priorities get divided by two? So just depends in this, like you can have hard coded priorities that don't change, you can have them dynamically change, just depends on the algorithm. All right, so let's have fun with virtual memory. So we can actually control our processes virtual memory. So there's this cool system call that is actually cool called memory map. The system call is called nmap because of course we have to make everything shorter. So it is used to map files. You can use it to map files to a processes virtual address space. So has anyone like opened a file and then you have to do read system calls to it and it kind of sucks, right? Because you need to like declare a buffer and all that stuff. Well, this actually makes it a lot easier to actually read a file. So let's just see what that API looks like. Actually, let's just go into an example. All right, so if I was to read a file with like normal system calls, I have to like do open, I get a file descriptor back and I just constantly do read system calls until eventually it returns zero. And then I know I'm at the end of the file, I have to create a buffer. I have to do all that fun stuff. It's no good. So instead, we can actually do something called, I mean, memory mapping a file or M mapping it. So here, we'll be a bit meta. So I'm going to open the file mmap.c, which is this file. So it's the current source file. So I'll open it as just read only, so I want to read it. Of course, we should be able to guess what file descriptor number we get because file descriptor zero, standard in, file descriptor one, standard out, file descriptor two, standard error. So next available should be three. So I'll just write a little assert there to just formulate or not formulate. Just make sure that what I think is true is actually true. And then we can see this other fun system call. So there's a struct called a stat. So it just has a bunch of information about a file. And the system call fstat will fill it. So this is another system call. So we do fstat for the file descriptor, give it the address of that struct. And then it goes ahead and populates it for us. And the reason we're doing this is because one of the things it will tell us is the size of the file. So once we know the size of the file, then we can set up this M map. So this takes six arguments, which is a lot, but we can go through them. So the first argument, the address, it's a bit silly. So you can actually politely ask the kernel exactly what virtual address you get as a result. If you want, if you say null, you let it pick it for you. So we will let it pick it. Next is the length, so the length of the mapping. So I'll just map the entire length or size of the file. Next are some permissions. So what you're allowed to do with that memory. So if I have prod read, that means I'm only allowed to read that information. I can't write to it. If I write to it, I would say fault. And I'm just making sure that I actually just read the contents of the file without accidentally writing to it or modifying it. Next is the flags, so map private. What that means is what happens when this process gets forked. So if it is map private, that means it is private to this process. So when you fork, the new process can't access the same virtual addresses. Because they're mapped directly to the file, otherwise you'd be mapping the same things. If you want to, there is a flag here called map shared. And that will make sure that when you fork, doesn't change anything. Both processes see the same thing. And that's a way to actually share memory if you want. Then the next is the file descriptor. So the file descriptor to actually map, and then the offset. So how many bytes into the contents of that file descriptor do you want to start? So we'll start at the beginning. So Mmap kind of looks like malloc a bit where it just returns a pointer. So we just check, hey, is it the special Mmap field? If it is, we don't handle an error or anything. We just have our assert handle it. But if we wanted to, we could check error no and all that stuff. And then after that, I don't need the file descriptor anymore. I mapped it. That's all the file descriptor was good for. So I can close it. Now, instead of doing read system calls or anything like that, well, I have a virtual address now. And what that points to is the contents of the file. So in order to read the file, I just access memory now. So I just have a for loop that goes over every single byte of the file and then just prints the character. So just a normal run of the mill for loop. And that's it. So if I go ahead and I run this, I see boom. I see the contents of the file and I don't have to do any read system calls or anything like that. Kind of cool, huh? Yeah, yeah, yeah, so yesterday I talked about using mmap to allocate memory. So this file descriptor here, you can set it as negative one and then it's not represented by a file. It just gives you some new memory. Yeah, so right now it's setting up those virtual pages and it will map those to the file. So it will make sure that the virtual address we get back, if we try to access that memory, we'll actually be accessing the file. Instead of just random physical memory. So if I don't give it a file descriptor, then that memory won't actually correspond to the contents of a file or anything. It'll just correspond to just random physical memory. And yeah, so that's one way you can share. Like that's one way you can do IPC across processes. So if I change FD to negative one, so it's actually memory and then change that flags from private to shared and then I forked. Both processes have the same virtual addresses that map to the same physical memory, so they can actually share memory. And you can communicate with them through memory, which is a lot faster because, well, you just need to do a map system call to set it up. And then after that, you don't have to do any slow system calls.  So that's mostly what we do. And that's fast, but kind of weird, weird and error prone. All right, any more questions about this or any weird things we want to do with it? But this is kind of cool, right? Yeah, yeah, so the offset is with respect to the contents of the file. So zero just means start at the beginning of the file. If I changed it to, I don't know, let's change it to offset then I have to recalculate my size, so I'm not going to change it, but yeah. Yeah, so the question is for the flags. Can it either just be read and read only or write only? So you can combine them. So you can combine them. The usual things with these flags are like their bit flags. So you can go ahead and do something like bitwise or and you can do things like that. So that will let you have that memory mapping for reading or writing or and writing, so it'll set both of them. Yeah, yeah, so the question is reading, writing the only two options and there is at least, so there's execute. So execute is like, you can see how this kind of exactly corresponds to the flags in the page table entry too, right? So like there was, well, this would set the valid bit, but there'd be read, write, execute are the main ones, yeah. No, if I set it to execute, all that means is that if there are instructions there, I can execute instructions there. So it just means that those bits, I'm allowed to essentially point my CPU to it and start executing them. And if you didn't have this, so this would be like some voodoo crap magic thing where I just load some raw instructions in the virtual memory. And then I essentially say go to and say go start executing that. And if I set this flag, it'll happily start executing instructions. But if I don't set that flag, I'll probably get like, I'll either get a sag fault or I'll get illegal instruction or some type of error in my process will just, but yeah, only unless you are doing really low level operating system things, only psychopaths would use that flag. Or if you're like trying to hack into something, yeah. Yeah, so like adding the executable flag here doesn't make any sense because I mean they're ASCII values. So if I tried to execute it anyways, there'd just be random instructions that just happened to correspond to the ASCII numbers and it'll probably crash and burn anyways. So it doesn't really matter. But right, maybe I want to do right because maybe I want to just read over it and then change everything to, I don't know, an A or something like that, who knows. So if I do that, I don't get any errors. And if I remove the flag, I should see that bad things happen. So, sag fault, yay. So, protect, so as I'm now teaching the first year, segmentation faults are actually good because they're easier to debug than just random things occurring and then you have to try and figure it out, which I guess you're kind of learning in lab two. But, yeah, yeah, no, because I modified it after I printed it out, right? So I could have looped over it again and it would just been all A's. No, like the actual file, that's a good question. Hopefully it doesn't, yeah, so it doesn't overwrite the original file. Yeah, just copied it in the memory in your virtual address space and then you're just playing with the memory and it's fine. But I think there's some flags you can do to actually modify it and then write it back out. That's just what we did, right? We had it right and I wrote all A's to it. So right now it's just when that process is running and then it doesn't write it back out to disk or anything like that. Yeah, and I forget how to force it too, but this did not. But I think there's a way to force it too, but I don't know it off the top of my head. All right, so that's kind of cool and kind of weird. So, yeah, there's our example. Anything else weird you want me to do with it? Yeah, so in this case it didn't print out all A's because I changed the byte to an A after I printed it. Yeah, so if I do, right, so if I just looped over it again, then I'm going to see a bunch of A's. It looks like it's screaming. Yeah, the last what, sorry? The last one's screaming. So, unmap is basically so like, M-map is kind of like malloc and this is kind of like free. Yeah, so it's just saying I'm done with it, undo that memory mapping. And then if I try and access again, I sag fault, right?  All right, any other questions about that? Okay, so let's try and answer, let's be smart. So, M-map is actually lazy, especially even if you don't like back it with a file or anything like that. So all that M-map call will do is set up the page tables. Whenever you do that, it doesn't actually read from the file. So what it actually does is it creates an invalid page table entry during that M-map call and it sticks some information there that's like, okay, if someone tries to access this, they actually want to access this file through this file descriptor. So it just stores some information in that page table entry. Again, like where on disk is this entry? And then the first time we try to access that page, our M-MU would call as like a page fault because it wouldn't be valid, right? Well, instead of just passing a sag fault up to your program, the kernel and essentially its interrupt handler can be like, okay, you faulted at this page table entry. I know that's supposed to be the file. So I'll just go ahead and read a page worth of content from that file. And I'll map it and like I'll fix up the entry, read it into a physical page, change the mapping to point to that physical page, and then just let the process resume again. And then now you're actually accessing the contents of the file through that. And this actually ensures that only the parts of the file that get read, like only the parts of the file you use actually get read. So in this case, if that file was like absolutely ginormous and say, I don't know, it was who knows, like 30 gigabytes. And I accessed the first byte on that while that would be on a page. And it would be like, okay, well I need to read a page of content from that file into memory and then set that up. And then if you wanted to just read byte, I don't know, 10 million. That would be on a particular page and say, okay, I'll go ahead and read that page now, and read it into physical memory, fix up that mapping. And then suddenly, all you have to do is just, yeah, all you have to do is you just read two pages. Doesn't matter how big the file is. So if we go back to that question, how is this possible? So it could be a 20 gig file, 30 gig file. How is it possible that it only uses like 5.8 or 6.8 gigs of real memory? So the reason because this is, well, whoever wrote the first version of the process or program just loaded the entire model into memory, whether or not you used it or not. And then some very smart person who has probably taken a course like this or at least played around with the kernel was like, okay, well, when you do model inference, they're typically sparse, you don't use all of it. So instead of reading the contents of a file that has contents I might not touch, what did they do? They M mapped it. So they just did like a one line change where instead of doing read system calls over and over again, they just did one M map system call. That was it and that's it. They just M mapped it. That was the whole wizardry behind that. So questions about that wizardry. So they immediately cut off like was like 24 gigs of memory that the process used by just doing M map. Pretty good trade off for having to sit through and listing to me drone on about virtual memory for a while. So you can actually get some real savings from it. And yeah, so the question, why would M map use less memory here than read? So again, this is a sparse file so we wouldn't actually access all of it. So all we have to do is actually set up the page tables. So M map, again, like I said, all that does is set up the page tables. So I know if I try and access that memory, where I actually have to load things into physical memory. So if we have a 20 gig file, can we come up with how much space we would actually need for the page tables and do a bit of review? Everyone likes virtual memory, right? So I'm M mapping a 20. Let's just say 20 gigabyte file for the sake of argument. So how many pages do I need to map a 20 gig file? Assuming, yeah, no, just how many pages do I need? Yeah, so kind of. So this number, so like 20 times 2 to the 30 divide by what? Divide by 2 to the 12. Yeah, so that's how many pages I would need, right? So that's like 20 times 2 to the 18, which is some weird number. So that is how many pages I need. How much space would be consumed by the page table entries are just required for this? So each page table can hold to the nine, but just in terms of how many page table entries I need just to keep track of all these pages. So like across all my L1s, how many page table entries do I need? This, right? And how much space do they take up? So they'll take up this times eight because it's eight bytes for page table entry. So in total, we have about 40 megabytes. So 40 megabytes for the page tables is the answer someone gave on this post. So is that actually true? I guess no, right? Because these are all the entries that are in the L0 page tables. How many L1s would I need in the best case? Yeah, couple kilobytes worth. Yeah, exactly how many, yeah. Yeah, it's essentially exactly this much, like this many pages, divided by 512, right? Because, well, they can only hold 512 each. So it would be like this many pages divided by 512, which would be like 20, I think it is. Which, yeah, that is a lot. And then, well, we only have one L2 page table that we can assume to be there. So this 40 is approximately right. But if you want to be technically correct, then you have to keep track of the L1 page tables and be a giant nerd, I guess. So the person who said 40 megabytes exactly was wrong because they didn't account for L1s. So slightly higher than that. So if I have 20 full L1 page tables, well, in actuality, I will have like, so this number is, I think this is like my L1, L0 page tables. My full ones, or wait, no, sorry, that one. So I use 40 megabytes for my four L1 page tables. And through that, there are 10,240 full L0 page tables and then 20 full L1 page tables. So my grand total, I would need actually 10,260 page tables. And then the size of each one of those are going to be 4,096. And if I take that to a megabyte and divide that whole thing by what a megabyte is to the 20, you'll see that, actually, it's like 40.08 megabytes. Wow, what a difference, right? Why did we do all that when we just approximated it? I don't know. I'm slightly crazy. But it kind of makes sense, right, that I would have, so if I have like a 39-bit virtual address space, that means I can address up to 512 gigabytes. And that makes sense that if I need to map 20 gigabytes, well, that's essentially like 20 entries in my L2 page table. So each entry can actually map up to a gigabyte. So it kind of makes sense. So each entry in the L2 can, if everything is full, figure out the mappings for up to a gigabyte of memory. And here, if you want, so yeah, a question, if we want to be specific, should we also count the L2 page table? So every process needs to have an L2 page table to exist, so you can assume it already exists. Otherwise, that process wouldn't be running. All right, any other questions or anything like that? All right, so I will be here around and use the extra time if you want to ask me about your problems with Lab 2. So just remember, we're all in this together.